The full story is that I'm parsing some documents in Excel spreadsheets and I just wanted to simplify the expresions, so the list c('A1'),c('A'),c('B') just means that the value at the constant cell A1 is followed by the values of columns A and B at the "current row" in the details area, without having to include the iterator variable as in c('A',1),c('A',$i),c('B',$i) (which is also supported). The problem was that I always got the values at a fixed row based on the previous value of the iterator variable when it was not explicit as an argument, either as c('A',$i) or c("A$i").

Of course, there are also some helper functions to get formated values. For instance, d('A',1) internally calls c(@_) and returns a date properly formatted (and not an epoch number). As there could be many levels of nested functions, I decided to use a global variable and avoid the optional parameter.

Now I know that for over lists does a special treatment on the iterator variable, probably because of a perl 4 inheritance, and I should use a C-like for or a while construct when using a global variable as iterator from subroutines called from the loops, but for the moment, I'm using this construct:

my $row = 0; for (2..10) { $row = $_; gen(d('A1'), c('A'), c('B'), t('C'), c('D')+c('E')); }

I think it is much more clear and easy to maintain when the spreadsheet changes than:

my $row = 0; for ($row = 2, $row <= 10; $row++) { gen(d('A',1), c('A',$row), c('B',$row), t('C',$row), c('D',$row)+c(' +E',$row)); }

Now I see that if I had declared the global variable without an initial value of zero, an error should have been raised and saved many hours trying to identify why I got wrong values from the spreadsheet. >:-(


In reply to Re^5: Access a global variable from a subroutine when used as "for" iterator by vitoco
in thread Access a global variable from a subroutine when used as "for" iterator by vitoco

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.