In general I think you will discover that:
1) regex is better than pack/unpack (except for specific cases where you know that columns are guaranteed to line up - and even then just use regex to skip columns and use list slice to get what you want. Of course pack/unpack are necessary when editing binary files, but in general this is not the right way to go.
2)indexed variables are almost never necessary in Perl. One magic thing of Perl is it reduces the probability of "off by one errors"
3)prefer foreach (@array){..} over any kind of C style for loop. I write one C style for loop per about 5K lines of Perl.
I think you just need to get the right regex to feed a simple loop and that will do what you want.#!usr/bin/perl -w use strict; while (<DATA>) { my $last_first_digit = ($_=~ m/(\d)\d*\s*$/)[0]; # print "$last_first_digit\n"; #for debugging print "<exML LOLZ = \"$last_first_digit\"/>\n"; } __DATA__ HAI WORLD Times 0 HAI WORLD Times 1 HAI WORLD Times 2 HAI WORLD Times 3 HAI WORLD Times 4 HAI WORLD Times 5 HAI WORLD Times 6 HAI WORLD Times 7 HAI WORLD Times 8 HAI WORLD Times 9 HAI WORLD Times 10 HAI WORLD Times 11 HAI WORLD Times 12 HAI WORLD Times 13 HAI WORLD Times 14 HAI WORLD Times 15 HAI WORLD Times 16 HAI WORLD Times 17 HAI WORLD Times 18 HAI WORLD Times 19 HAI WORLD Times 20 HAI WORLD Times 21 HAI WORLD Times 22 HAI WORLD Times 23 HAI WORLD Times 24 HAI WORLD Times 25 ============== this prints: <exML LOLZ = "0"/> <exML LOLZ = "1"/> <exML LOLZ = "2"/> <exML LOLZ = "3"/> <exML LOLZ = "4"/> <exML LOLZ = "5"/> <exML LOLZ = "6"/> <exML LOLZ = "7"/> <exML LOLZ = "8"/> <exML LOLZ = "9"/> <exML LOLZ = "1"/> <exML LOLZ = "1"/> <exML LOLZ = "1"/> <exML LOLZ = "1"/> <exML LOLZ = "1"/> <exML LOLZ = "1"/> <exML LOLZ = "1"/> <exML LOLZ = "1"/> <exML LOLZ = "1"/> <exML LOLZ = "1"/> <exML LOLZ = "2"/> <exML LOLZ = "2"/> <exML LOLZ = "2"/> <exML LOLZ = "2"/> <exML LOLZ = "2"/> <exML LOLZ = "2"/>
In reply to Re: Function call in regex replacement string
by Marshall
in thread Function call in regex replacement string
by PoorLuzer
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |