How to sort an array in Bash

You don’t really need all that much code: Supports whitespace in elements (as long as it’s not a newline), and works in Bash 3.x. e.g.: Note: @sorontar has pointed out that care is required if elements contain wildcards such as * or ?: The sorted=($(…)) part is using the “split and glob” operator. You should … Read more

Multidimensional Vectors in C++

If you are able to use C++11, multidimensional arrays and vectors of vectors can be initialized in a similar manner. However, there are differences that must be understood to access the elements without running into undefined behavior. For a multidimensional array, memory for the elements of the array is required to be allocated contiguously. For … Read more

type any? has no subscript members

When you subscript profile with “Addresses”, you’re getting an Any instance back. Your choice to use Any to fit various types within the same array has caused type erasure to occur. You’ll need to cast the result back to its real type, [[String: Any]] so that it knows that the Any instance represents an Array. Then you’ll be able to subscript it: This is very … Read more

Weighted random selection from array

Compute the discrete cumulative density function (CDF) of your list — or in simple terms the array of cumulative sums of the weights. Then generate a random number in the range between 0 and the sum of all weights (might be 1 in your case), do a binary search to find this random number in … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)