in reply to populating a HoA with regexp

You could do this:

#! perl -slw use strict; use Data::Dump qw[ pp ]; my %dfs; m[([a-z]+)(\d+)] and push @{ $dfs{ $1 } }, $2 while <DATA>; pp \%dfs; __DATA__ brain0001 lung0001 brain00002 kidney0003 brain00003

Outputs:

[14:52:38.81] C:\test>junk58 { brain => ["0001", "00002", "00003"], kidney => ["0003"], lung => ["0001"], }

But maybe you don't want to load up anything you are not expecting? Maybe you want a warning for lines that don;t match your regex?


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

RIP Neil Armstrong

div

Replies are listed 'Best First'.
Re^2: populating a HoA with regexp
by asqwerty (Acolyte) on Aug 28, 2012 at 14:07 UTC

    Thanks!

    Actually I want to load only the words I'm expecting, but your answer (and the previous reply) gave me the hint I need