Some time ago I wrote a subroutine which takes a number of arrays named @out_a @out_b @out_c etc. and gets one entry at a time in parallel from a specified subset of these arrays to write into a tab-delimited line which is added to a single output array. This output array was then used to load an Excel spreadsheet using the "Import Data" function. The parameter passed to the subroutine is an array of letters which designate the names of the arrays to be used for input. The input arrays are all different lengths. (I have since changed the code to write the data directly into the spreadsheet using OLE stuff but that is not the point here.) In the process of writing this code I discovered a very subtle distinction between "my" and "local" variables which was not initially obvious to me.
I don't know if this is the correct place to post this but it is not a question so I am posting it here. There are obviously a number of ways to improve the code but again the point is the subtle difference between "my" and "local".
sub meld_columns {
my (@cols_to_meld) = @_;
my ($ctm, $arrnm, $aline);
my @outarray;
# "local" needed for the following vice "my" because
# the indirect references [$$ctm] require access to
# the global symbol table and "my" variables are not
# in the global symbol table.
local ($a, $b, $c, $d, $e, $f, $g, $h);
# Set column indicators all to -1 (eol)
$a = $b = $c = $d = $e = $f = $g = $h = "-1";
# then set the ones we are processing to 0
foreach $ctm (@cols_to_meld) {
$$ctm = 0;
}
while (($a >= 0)||($b >= 0)||($c >= 0)||($d >= 0)||($e >= 0)||($f
+>= 0)||($g >= 0)||($h >= 0)) {
$aline = '';
foreach $ctm (@cols_to_meld) {
$arrnm = 'out_'."$ctm";
if (($$ctm <= $#$arrnm) && ($$ctm ne "-1")) {
$aline .= "$$arrnm[$$ctm]\t";
$$ctm++;
$$ctm = "-1" if ($$ctm > $#$arrnm);
} else {
$aline .= "\t\t";
$$ctm = "-1";
}
}
$aline =~ s/(.*)\t$/$1/; # get rid of trailing tab
push @outarray, ("$aline");
}
return @outarray;
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.