mr. jaggers has asked for the wisdom of the Perl Monks concerning the following question:

Better monks than I,

So, I've got this input file name in $ARGV[0], and then three-tuples follow that, composed of an output file, and two csv lists of numbers (I call them maps) :
/bin/perl test.pl input out1 0,2,4,6 0,1,2,3 out2 1,3,5,7 0,1,2,3
Okay, so here is a test loop to populate an array of hashes, each hash containing some useful stuff about each three-tuple.
for ($ii=1;$ii<=@ARGV-1;$ii+=3) # three-tuples (outfile, map1, map2) { $files[(($ii-1)/3)]{fname}=$ARGV[$ii]; $files[(($ii-1)/3)]{fhandle}= new IO::File ">".$files[(($ii-1)/3)]{f +name}; $files[(($ii-1)/3)]{map1}=[split /,/,$ARGV[$ii+1]]; print "map1 : [" . join /,/,$files[(($ii-1)/3)]{map1} . "]\n"; print Dumper(@files); }
Well, the wierd thing is that the second to last print statement prints something like:
map1 : [ARRAY(0x1dc518c)] $VAR1 = { 'fname' => 'out1', 'map1' => [ '0', '2', '4', '6' ], 'fhandle' => bless( \*Symbol::GEN0, 'IO::File' ), }; . . .
... instead of:
map : [0,2,4,6] . . .
I've tried adding various brackets and parens (admittedly, not the best way to debug) to try to ensure proper context, but to no avail.
Yes, I know that I could simply print $ARGV[$ii+1] but that really wasn't the point. Ignoring stylistic and readability concerns (those will be woven into working code, not this cruft) what's wrong with the code?

Replies are listed 'Best First'.
Re: Join returning array reference, not string... why?
by Zaxo (Archbishop) on Dec 24, 2002 at 02:39 UTC

    You need to dereference $files[($ii-1)/3]{map1]. It contains a reference to an array, so

    print "map1 : [", join( ',', @{$files[(($ii-1)/3)]{map1}}), "]\n";
    will fix it. I changed join's first argument to be a string, and replaced concatenation with a print list.

    After Compline,
    Zaxo

      Now I understand... join got confused too. Okay, thank you much!
Re: Join returning array reference, not string... why?
by gjb (Vicar) on Dec 24, 2002 at 02:28 UTC

    You want to print @{$files[(($ii-1)/3)]{map1}} rather than $files[(($ii-1)/3)]{map1} since the latter is a reference to a list and the former converts it to a list the join function expects.

    Hope this helps, -gjb-

      Yeah, that was one of those bracketed things that I tried, and what that did was return the array, but join only used the scalar context and printed:
      map : [4] . . .
      ... that's part of why I got so puzzled. Thanks, though!