in reply to Re: Read two files and print
in thread Read two files and print
1. split /\s+/, $line; splits on any whitespace character, this includes space,\f,\r,\n,\t. Since \n is in this set, you don't need to chomp($part2); doesn't hurt but it is not necessary here. The reason in previous post that "\t" didn't work is that you need a regex for the first arg to split./\t/ would have worked but /\s+/ is usually better. The \t idea would result in a \n in $part2 and of course since you can't see these non-printing characters it is possible that there is are some plain spaces in there!
2.The best way to get the 2nd thing from the split is with list slice. my $part2 = (split /\s+/, $line)[1]; Since you don't use $part1, there is no need to assign it. It often occurs that you are working with a line with a bunch of things on it and you just want a couple of them. Using list slice allows you to assign meaningful names to these things like maybe: my($temperature,$city)=(split /\s+/,$line)[3,8];. This is a lot better than say, $line[3] because you don't need any comments to explain that thing 3 means temperature.
Of course here the op probably has some other name in mind for $part2 that would make the code even more clear.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Read two files and print
by sandy1028 (Sexton) on Feb 27, 2009 at 04:20 UTC | |
by GrandFather (Saint) on Feb 27, 2009 at 05:11 UTC | |
by Marshall (Canon) on Mar 01, 2009 at 16:37 UTC |