Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Can't use string as an ARRAY ref

by Anonymous Monk
on Aug 21, 2015 at 14:59 UTC ( [id://1139425]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

I am stuck on this one where I am loosing one level in the array once I process the DATA inside of the "for loop". I hope this sample code shows better what I am trying to do and that someone can help me solve this one.

Code Sample:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; ... foreach my $num ( keys %{$data} ) { my $main = $data->{$num}{main}; unshift @$main,["Account", "Name", "Date", "Code #"]; warn Dumper $main; =code The data structure looks like this: $VAR1 = [ [ 'Account', 'Name', 'Date', 'Code #' ], [ '12345', 'MONICA', '01/01/1900', '0X10' ], [ '000001', 'MARY L', '01/01/2000', '0111P' ], [ '8884', 'JOHN M.', '01/01/1932', '0OK8' ], ]; =cut my $type = $data->{$num}{type}; unshift @$type,["HOUSE", "YEAR", "COLOR",]; warn Dumper $type; =code The data structure looks like this: $VAR1 = [ [ 'HOUSE', 'YEAR', 'COLOR', ], [ 'Main', '1900', 'Red', ], [ 'APT', '1290', 'Blue', ], [ 'AVAL', '1921', 'Green', ], ]; =cut my $princ = $data->{$num}{princ}; unshift @$princ,["AC #","Name", "DATE", "Ref","Case #1","Case #2", "C +ase #3", "Case #4",]; warn Dumper $princ; =code The data structure looks like this: $VAR1 = [ [ 'AC #', 'Name', 'DATE', 'Ref', 'Case #1', 'Case #2', 'Case #3', 'Case #4' ], [ 'Q3.0', 'OK', '1900-01-01', 'N', 'O', 'O', 'X', 'Y' ], [ '12w', 'PL', '2000-01-02', 'N', 'P', 'O', 'X', 'A' ], ]; =cut for my $i ( 0 .. $#{ $main } ){ # more stuff my ($a, $b, $c) = add_more( data => $main->[$i], ); my ($d, $e, $f) = add_more( data => $type->[$i], ); my ($g, $h, $j) = add_more( data => $princ->[$i], ); } } sub add_more{ my (%args) = @_; my $data = $args{data} || ''; warn Dumper $data; =code # I see that one level is lost here, just cant figure it out how to fi +x it $VAR1 = [ 'Account', 'Name', 'Date', 'Code #' ]; Can't use string ("Account") as an ARRAY ref while "strict refs" in us +e ... =cut ... column_props => [ map{ { justify => 'center', } }1..@{$data->[0]}], ... }

Thanks for looking!

Replies are listed 'Best First'.
Re: Can't use string as an ARRAY ref
by GotToBTru (Prior) on Aug 21, 2015 at 15:07 UTC

    $princ is an AoA, an array whose elements are array refs. You are passing one of those array refs to add_more, which you store as $data. However, $data->[0] is a scalar, not an array ref, hence the error message.

    Dum Spiro Spero
      I know, I just can't think on how to fix it.

        It looks like you might be trying to construct a table, possibly in HTML. I would suggest looking into the CPAN modules for that purpose. They will dictate your data structure, at least to an extent.

        Otherwise, what do you want the output to look like?

        Dum Spiro Spero
Re: Can't use string as an ARRAY ref
by tangent (Parson) on Aug 21, 2015 at 15:52 UTC
    I think 1..@{$data->[0]} in your add_more sub is what is giving you the error, it should be:
    column_props => [ map { { justify => 'center' } } 1..@{$data} ],
      I guess I have to find a way to pass these:
      data => $main->[$i], and data => $type->[$i], and data => $princ->[$i] +,

      as a reference, right?
Re: Can't use string as an ARRAY ref
by 1nickt (Canon) on Aug 21, 2015 at 15:07 UTC

    You should write a small test script with the simplest possible data and structure to demonstrate the problem. Most likely you'll figure it out, but if not you can post the script here and people will be able to see what is going on.

    The way forward always starts with a minimal test.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1139425]
Approved by 1nickt
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-24 23:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found