I'm not quite sure what needs to be accomplished here. Consider the following code. Note that within a regex, you can use a $var in place of a fixed regex. s/$find/$replace/;

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.

#!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"/>
I think you just need to get the right regex to feed a simple loop and that will do what you want.

In reply to Re: Function call in regex replacement string by Marshall
in thread Function call in regex replacement string by PoorLuzer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.