First, I would to see some comparsion to split and why your way is better (which it is).
Second, why not do:
while (my ($line) = $record->{data} =~/(.*)\n/g) {
...
}
It saves on the chomp and is more concise, imho ...
Being right, does not endow the right to be rude; politeness costs nothing. Being unknowing, is not the same as being stupid. Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence. Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.
| [reply] [d/l] |
That doesn't work, because you're changing the context of =~ from scalar to list. while ($record->{data} =~/(.*)\n/g) { my $line = $1; would be the equivalent without the chomp.
However, neither solution will always work with Windows text files. See my reply to this post's grandparent.
| [reply] [d/l] |
It saves on the chomp and is more concise, imho ...
Yep, you're right. I was tired, and in too much of a hurry
sigh...
| [reply] |
Unlike unix, it's common but not standard to put a \n on the last line in DOS/Windows. <FILE> handles the missing \n, but your code chops off the last line. The following handles both the presence and absense of the last \n:
#!/usr/bin/perl -w
use strict;
my @files_to_process= (
{data=>"The quick\n" .
"brown fox\n" .
"ran over\n" .
"the purple\n" .
"witch. So\n".
"there!"}
);
foreach my $record (@files_to_process) {
while ($record->{data} =~/(.*\n|.+$)/g) {
chomp(my $line = $1);
print "[$line]\n";
}
}
| [reply] [d/l] |
this worked perfectly thanks. I just have a question about how it works? it seems like the assigments of $1 remove that section from the original variable. is that correct? can someone explain how that works in order to keep the pointer for the while in the correct place.
thanks soo much you were all a great help! | [reply] |
Not quite. The g forces subsequent calls to the regexp to match from somewhere after the previous match ended, until no match is found.
print("$1:") while ('abcdefgh' =~ /(.)/g);
a:b:c:d:e:f:g:h:
print("$1:") while ('vowels' =~ /([aeiou])/g);
o:e:
\G is similiar to ^, but refers to where the last match left off.
print("$1:") while ('oiseau' =~ /\G([aeiou])/g);
o:i:
| [reply] [d/l] [select] |