tbone654 has asked for the wisdom of the Perl Monks concerning the following question:
This is driving me crazy... I'm ultimately trying to read in two lists, one with a single column of ordered names... The second list is a column of names with a comma separated (this test script is using semi-colons) value after the name...
If I were able to do this in unix I would be:
Which is ok, but file1 loses it's order to file2, and what I really want to do is print values from file2 next to the order in file1... so I need to split off values and print the results next to the members of file1 where there is a match...grep -f file1 file2
my $a; my @foo = qw/tom steve bill roger bob/; my @bar = qw/roger;99 steve;56 ted;88 tom;54/; for($a=0;$a<@foo;$a++) { printf("%s %s\n",$foo[$a],grep(/$foo[$a]/,@bar)); } print "----\n"; for($a=0;$a<@foo;$a++) { # printf("%s\n", grep(/$foo[$a]/,@bar) ); printf("%s\n", (split /;/, grep(/$foo[$a]/,@bar))[0] ); } --output before split-- tom tom;54 steve steve;56 bill roger roger;99 bob ---- tom;54 steve;56 roger;99 --output using split-- tom tom;54 steve steve;56 bill roger roger;99 bob ---- 1 1 0 1 0
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: split (grep)
by Laurent_R (Canon) on Oct 23, 2014 at 20:35 UTC | |
by tbone654 (Beadle) on Oct 23, 2014 at 21:30 UTC | |
by tbone654 (Beadle) on Oct 24, 2014 at 18:33 UTC | |
|
Re: split (grep)
by toolic (Bishop) on Oct 23, 2014 at 20:04 UTC |