I was wondering when you get time could you break this down for me?:
print "$1$_$4" for $2..$3;
I'm having a hard time grasping it....
OK, I'll explain how the whole program works. (I'm CCing the list, other people could benefit as well.) Here's the program:
#!/usr/bin/perl -nlw if (/(.*)\b(\d+)-(\d+)\b(.*)/) { print "$1$_$4" for $2..$3; } else { print; }
The -n switch means that the body of this program is in the implicit while(<>) loop, i.e. it's run for every line of input (the input can be read from files with names given as program arguments or from standard input, just like with traditional Unix filters like sort or grep).
Read more about -n switch in perlrun(1) manpage and read about the null filehandle <> in I/O Operators section of perlop(1) manpage.
http://www.perldoc.com/perl5.8.0/pod/perlrun.html#-n http://www.perldoc.com/perl5.8.0/pod/perlop.html#I-O-Operators
If a given line matches the regular expression /(.*)\b(\d+)-(\d+)\b(.*)/ then four parts of the line are stored in $1, $2, $3 and $4 variables (one for every pair of capturing parentheses). See perlre(1):
http://www.perldoc.com/perl5.8.0/pod/perlre.html
Now $1 contains everything before the first number of the number1-number2 pair, $2 contains the first number, $3 the second number and $4 is everything after those numbers.
The "for $2..$3" means to run the print function for every number in the range between $2 and $3 inclusive (this double dot is a range operator, see perlop(1) manpage) with this number stored in the $_ variable every time the print is run. See Foreach Loops in perlsyn(1).
http://www.perldoc.com/perl5.8.0/pod/perlsyn.html#Foreach-Loops http://www.perldoc.com/perl5.8.0/pod/perlop.html#Range-Operators
If you are familiar with the C syntax, then:
print "$1$_$4" for $2..$3;
is actually equivalent to this C-style for loop:
$text_before_nums = $1; $num1 = $2; $num2 = $3; $text_after_nums = $4; for ($i = $num1; $i <= $num2; $i++) { print $text_before_nums, $i, $text_after_nums; }
Every print implicitly prints a "\n" thanks to the -l switch in the #! line. See perlrun(1):
http://www.perldoc.com/perl5.8.0/pod/perlrun.html#-l%5boctnum%5d
-zsdc.
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]