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?
Back to
Seekers of Perl Wisdom