How would you push each id value in an array from an array of hashes?
I have this array:
@friends = [
{'id' => 1, 'last_name' => 'Fo', 'first_name' => 'fa' },
{'id' => 3, 'last_name' => 'pa', 'first_name' => 'pi' },
{'id' => 2, 'last_name' => 'ma', 'first_name' => 'mi' }
];
I want to create an array of value id like this: @friend_ids = [1, 3, 2], using push.
You probably have
@friends = ( ... )and want@friend_ids = (1, 3, 2). Square brackets are used for array references, not lists.You can create such an array like this:
But you can achieve the same without push more easily:
If you need to remove duplicates, and sort the keys, you can use:
if all IDs are numbers or just
if some IDs are not numeric (
uniqis from List::MoreUtils).