in reply to my array is almost a hash, but keys are not unique.

If you eschew the C for loop where possible, and don't mind destroying the source array, then you can:

use strict; use warnings; use Data::Dump::Streamer; my @in = (one => 1, two => 2, two => '2.003'); my %hash; while (@in > 1) { my ($key, $value) = splice @in, 0, 2; push @{$hash{$key}}, $value; } Dump \%hash;

Prints:

$HASH1 = { one => [ 1 ], two => [ 2, 2.003 ] };

True laziness is hard work

Replies are listed 'Best First'.
Re^2: my array is almost a hash, but keys are not unique.
by Boldra (Curate) on Apr 07, 2009 at 14:20 UTC
    It's true; I do eschew. Don't you?


    - Boldra

      Indeed I do. The Perl for/foreach loop is generally much clearer than the C for loop, and is almost never bug bait (the C for loop is prone to off by 1 errors).

      It's the 'destroying the source array' part that may be more of an issue if the array is large enough that you might think twice about copying it and are into premature optimization.


      True laziness is hard work