Which has faster performance indexesOfObjectsPassingTest or filteredArrayUsingPredicate?

The following tests (compiled in Release mode, executed on a Mac Pro) indicate that filteredArrayUsingPredicate is slower than indexesOfObjectsPassingTest if you use a “textual” predicate, but faster if you use block-based predicate. The fasted method in my test was a simple (fast-enumeration) loop that adds all matching objects to a mutable array. Results for filtering … Read more

Compare two 2D arrays & get intersection and differences

Convert arrays to a format, where array index is the sight_id: $b1 =array(); foreach($a1 as $x) $b1[$x[‘sight_id’]] = $x[‘location’]; $b2 =array(); foreach($a2 as $x) $b2[$x[‘sight_id’]] = $x[‘location’]; Calculate the differences and intersection: $c_intersect = array_intersect_key($b1,$b2); $c_1 = array_diff_key($b1,$b2); $c_2 = array_diff_key($b2,$b1); Convert arrays back to your format: $intersect_array = array(); foreach($c_intersect as $i=>$v) $intersect_array[] = … Read more

LINQ Where Ignore Accentuation and Case

To ignore case and accents (diacritics) you can first define an extension method like this: public static string RemoveDiacritics(this String s) { String normalizedString = s.Normalize(NormalizationForm.FormD); StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < normalizedString.Length; i++) { Char c = normalizedString[i]; if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark) stringBuilder.Append(c); } return stringBuilder.ToString(); } (Modified … Read more

How to auto select range from a filter without having to manually enter it?

You can get the filtered range dimensions from getFilter().getRange(). This will copy all the filtered range: function copycolA() { var sourceSheet = SpreadsheetApp.getActive().getSheetByName(‘Current’); var targetSheet = SpreadsheetApp.getActive().getSheetByName(‘A’); var sourceRange = sourceSheet.getFilter().getRange(); sourceRange.copyTo( targetSheet.getRange(‘A1’), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false); } To read: Filter#Range Related answer Note that .getValues() or other script operations will NOT get the filtered only, but … Read more

Remove all elements from array that do not start with a certain string

Functional approach: $array = array_filter($array, function($key) { return strpos($key, ‘foo-‘) === 0; }, ARRAY_FILTER_USE_KEY); Procedural approach: $only_foo = array(); foreach ($array as $key => $value) { if (strpos($key, ‘foo-‘) === 0) { $only_foo[$key] = $value; } } Procedural approach using objects: $i = new ArrayIterator($array); $only_foo = array(); while ($i->valid()) { if (strpos($i->key(), ‘foo-‘) === … Read more