in reply to passing @ to sub and getting back hash

You don't really need a sub to do that:
my %hash1 = map /^ "xxxx-(\d+):xxxxxx([^,]+),.+"$/, @dc;

Replies are listed 'Best First'.
Re^2: passing @ to sub and getting back hash
by GrandFather (Saint) on Dec 31, 2007 at 21:56 UTC

    Not quite so simple. Better would be:

    my %hash2 = map {/^ "xxxx-(\d+):xxxxxx([^,]+),.+"$/ ? ($1 => $2) : ( +)} @data;

    Perl is environmentally friendly - it saves trees
      How would that be "better"?
      $ perl -MData::Dumper -le' my @dc = ( "xxxx-123:xxxxxxABC,z", "xxxx-:xxxxxx,z", "xxxx-456:xxxxxx, +z", "xxxx-XXX:xxxxxxDEF,z", "xxxx-789:xxxxxxGHI,z" ); my %hash1 = map /^xxxx-(\d+):xxxxxx([^,]+),.+$/, @dc; my %hash2 = map { /^xxxx-(\d+):xxxxxx([^,]+),.+$/ ? ($1 => $2) : () } +@dc; print Dumper \%hash1, \%hash2; ' $VAR1 = { '123' => 'ABC', '789' => 'GHI' }; $VAR2 = { '123' => 'ABC', '789' => 'GHI' };
        Grandfather's statement is better because it gracefully(and explicitly) handles the case where the regex does not match.

             "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

      thanks guys, my script works like a charm, but Grandpa's map statement is very stylish(i put it into my /etc/motd for now) thanks!!!!