On Tue, 18 Oct 2011, Barry Brevik wrote:
> 
> I've been fooling around with this situation entirely too long. Below is
> a demonstration of the problem, but the real situation is part of some
> Win32::OLE code I am maintaining, so it's relevant <g>.
> 
> The problem is this: the 1st loop pushes each sub-array (for example
> "39", "3") onto @toparray. A print statement shows that it is "39", "3"
> (for example).
> 
> In the 2nd loop, I want to pull each sub-array as it was pushed on, but
> instead I get the first "this is line:" statement, and an error: "Can't
> use string ("39") as an ARRAY ref while "strict refs" in use at
> test42.pl line 39.".
> 
> I know this smells like a newbie problem, but I'm not a newbie;
> nevertheless, I guess I do not know enough about array references yet.
> 
> Barry Brevik
> 
> ==========================================
> use strict;
> use warnings;
> 
> my @bartypes =
> (
>   [
>     ['39',   '3'],
>     ['128',  '1'],
>     ['128A', '1A'],
>     ['128B', '1B'],
>     ['128C', '1C']
>   ],
> 
>   [
>     ['yes',         'A'],
>     ['no',          'B'],
>     ['maybe',       'C'],
>     ['ask later',   'D'],
>     ['answer hazy', 'E']
>   ]
> );
> 
> my @toparray = ();
> 
> foreach my $bartype (@bartypes)
> {
>   foreach my $record (@$bartype)
>   {
>     push @toparray, @$record;

Here you are flattening the @$record into @toparray.  If you wanted
to keep the nested array structure, you would have to push the array
reference instead:

      push @toparray, $record;

Or if for whatever reason you must create a copy of the data (e.g.
you want to modify it later, but keep the original intact), then
you put the copied values into an anonymous array and push that:

      push @toparray, [@$record];

>     print @$record, "\n";
>   }
> }
> print "\n\n";
> 
> foreach my $line (@toparray)
> {
>   print "this is line: $line\n";
> 
>   foreach my $cell (@{$line})
>   {
>     print "  cell: $cell\n";
>   }
>   print "\n";
> }

Cheers,
-Jan


_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to