asqwerty has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks,
I have a file with a single column of words like this:
brain0001 lung0001 brain00002 kidney0003 brain00003
I need to put this words into a hash of arrays, separated by its category (brain, lung, kidney), so I wrote this code:
$data = "mydata.list"; @pref = ("brain", "lung", "kidney"); %dfs = map {$_ => ()} @pref; open DF, "<$data"; while(my $line=<DF>){ chomp($line); foreach $pat (@pref){ push @{$dfs{$pat}},$line if ($line=~/$pat/); } } close DF;
This code actually works and populate the hash in the right way. However, I think the foreach loop can be replaced with a single line.
How can I do this?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: populating a HoA with regexp
by BrowserUk (Patriarch) on Aug 28, 2012 at 13:55 UTC | |
by asqwerty (Acolyte) on Aug 28, 2012 at 14:07 UTC | |
|
Re: populating a HoA with regexp
by Anonymous Monk on Aug 28, 2012 at 13:50 UTC | |
|
Re: populating a HoA with regexp
by Athanasius (Cardinal) on Aug 28, 2012 at 15:06 UTC | |
by asqwerty (Acolyte) on Aug 28, 2012 at 15:24 UTC | |
|
Re: populating a HoA with regexp
by linuxkid (Sexton) on Aug 28, 2012 at 20:44 UTC | |
by Kenosis (Priest) on Aug 28, 2012 at 21:46 UTC | |
by Anonymous Monk on Aug 28, 2012 at 22:04 UTC | |
by asqwerty (Acolyte) on Aug 29, 2012 at 08:05 UTC |