Printing out 3 dimensional arrays.

Discussion in 'Perl Misc' started by John, Dec 4, 2006.

  1. John

    John Guest

    Hello, just a little thing. I'm trying to print out a 3 dimensional
    array using the following code

    for(@AoAoA)
    {
    my @AoA =@{$_};
    for(@AoA)
    {
    my @array =@{$_};
    for(@array)
    {
    print $_
    }
    print "\n";
    }
    print "#################################\n";
    }

    but i get told that

    "Can't use string ("R03210") as an ARRAY ref while "strict refs" in use
    at graph.plx line 46."

    where line 46 is the line that says "my @array =@{$_};" but I'm sure
    that it is a 3 dimensional array
    that I am passing it at the start. Thanks.
    John, Dec 4, 2006
    #1
    1. Advertising

  2. John

    John Guest

    Cheer mate, that is a much better way of doing it.

    On 4 Dec, 22:42, "Todd W" <> wrote:
    > "John" <> wrote in messagenews:...
    >
    >
    >
    > > Hello, just a little thing. I'm trying to print out a 3 dimensional
    > > array using the following code

    >
    > > for(@AoAoA)
    > > {
    > > my @AoA =@{$_};
    > > for(@AoA)
    > > {
    > > my @array =@{$_};
    > > for(@array)
    > > {
    > > print $_
    > > }
    > > print "\n";
    > > }
    > > print "#################################\n";
    > > }

    >
    > > but i get told that

    >
    > > "Can't use string ("R03210") as an ARRAY ref while "strict refs" in use
    > > at graph.plx line 46."

    >
    > > where line 46 is the line that says "my @array =@{$_};" but I'm sure
    > > that it is a 3 dimensional array
    > > that I am passing it at the start. Thanks.This is why I only use $_ in map or grep.

    >
    > What is probably happening is once you enter the the first inner loop, $_
    > from the outer loop is clobbered with the first array item from the inner
    > loop.
    >
    > Also, Data::Dumper is nice for what you are trying to do:
    >
    > use Data::Dumper;
    > print( Dumper( \@AoAoA ) );
    >
    > Todd W.
    John, Dec 4, 2006
    #2
    1. Advertising

  3. John

    DJ Stunks Guest

    John wrote:
    > Hello, just a little thing. I'm trying to print out a 3 dimensional
    > array using the following code
    >
    > for(@AoAoA)
    > {
    > my @AoA =@{$_};
    > for(@AoA)
    > {
    > my @array =@{$_};
    > for(@array)
    > {
    > print $_
    > }
    > print "\n";
    > }
    > print "#################################\n";
    > }
    >
    > but i get told that
    >
    > "Can't use string ("R03210") as an ARRAY ref while "strict refs" in use
    > at graph.plx line 46."
    >
    > where line 46 is the line that says "my @array =@{$_};" but I'm sure
    > that it is a 3 dimensional array
    > that I am passing it at the start.


    I love posts like this. you're sure are you? how sure are you
    exactly? the reason I ask is because I'm 1250% sure you're not :)~

    have you read the posting guidelines for this group?

    -jp
    DJ Stunks, Dec 4, 2006
    #3
  4. John

    DJ Stunks Guest

    Todd W wrote:
    > "John" <> wrote in message
    > news:...
    > > Hello, just a little thing. I'm trying to print out a 3 dimensional
    > > array using the following code
    > >
    > > for(@AoAoA)
    > > {
    > > my @AoA =@{$_};
    > > for(@AoA)
    > > {
    > > my @array =@{$_};
    > > for(@array)
    > > {
    > > print $_
    > > }
    > > print "\n";
    > > }
    > > print "#################################\n";
    > > }
    > >
    > > but i get told that
    > >
    > > "Can't use string ("R03210") as an ARRAY ref while "strict refs" in use
    > > at graph.plx line 46."
    > >
    > > where line 46 is the line that says "my @array =@{$_};" but I'm sure
    > > that it is a 3 dimensional array
    > > that I am passing it at the start. Thanks.

    >
    > This is why I only use $_ in map or grep.
    >
    > What is probably happening is once you enter the the first inner loop, $_
    > from the outer loop is clobbered with the first array item from the inner
    > loop.


    I'm glad you said "probably" and not that you were "sure" like the OP.
    because that's not it.

    try it, here's an actual 3D array (since the OP didn't provide one):

    my @three_d = (
    [
    [ qw{ 1 2 } ],
    [ qw{ 3 4 } ],
    ],
    [
    [ qw{ 5 6 } ],
    [ qw{ 7 8 } ],
    ],
    );

    -jp
    DJ Stunks, Dec 4, 2006
    #4
  5. John

    Paul Lalli Guest

    John wrote:
    > Hello, just a little thing. I'm trying to print out a 3 dimensional
    > array using the following code
    >
    > for(@AoAoA)
    > {
    > my @AoA =@{$_};
    > for(@AoA)
    > {
    > my @array =@{$_};
    > for(@array)
    > {
    > print $_
    > }
    > print "\n";
    > }
    > print "#################################\n";
    > }
    >
    > but i get told that
    >
    > "Can't use string ("R03210") as an ARRAY ref while "strict refs" in use
    > at graph.plx line 46."
    >
    > where line 46 is the line that says "my @array =@{$_};" but I'm sure
    > that it is a 3 dimensional array
    > that I am passing it at the start.


    Uh-huh. You're sure. Perl's sure you're not. Perl is infinitely more
    likely to be correct than you are. I believe Perl. I don't believe
    you.

    Try giving some sample data. Try giving a short-but-complete program
    that we can run, as suggested by the posting guidelines.

    Paul Lalli
    Paul Lalli, Dec 4, 2006
    #5
  6. John

    John Guest

    I did have a look for posting regulations before posting, but couldn't
    find them. I suppose that I should have looked harder. Your right a
    short-but-complete program would be a more sensible and constructive
    way of presenting the query. But in spite of all my errors the Todd W
    managed to give me advice that solved my problem quicker than you
    managed to complain about my posts short-comings. So while you're
    right to correct my posting style don't you think this group and the
    world in general would be a nicer place with less unnecessary rude and
    patronising behaviour?


    Love, John.

    P.s.

    Also I'm 'sure' you'll be glad to know that it was a 3 dimensional
    array that was printed out after I followed Todd W advice.


    On 4 Dec, 23:09, "Paul Lalli" <> wrote:
    > John wrote:
    > > Hello, just a little thing. I'm trying to print out a 3 dimensional
    > > array using the following code

    >
    > > for(@AoAoA)
    > > {
    > > my @AoA =@{$_};
    > > for(@AoA)
    > > {
    > > my @array =@{$_};
    > > for(@array)
    > > {
    > > print $_
    > > }
    > > print "\n";
    > > }
    > > print "#################################\n";
    > > }

    >
    > > but i get told that

    >
    > > "Can't use string ("R03210") as an ARRAY ref while "strict refs" in use
    > > at graph.plx line 46."

    >
    > > where line 46 is the line that says "my @array =@{$_};" but I'm sure
    > > that it is a 3 dimensional array
    > > that I am passing it at the start.Uh-huh. You're sure. Perl's sure you're not. Perl is infinitely more

    > likely to be correct than you are. I believe Perl. I don't believe
    > you.
    >
    > Try giving some sample data. Try giving a short-but-complete program
    > that we can run, as suggested by the posting guidelines.
    >
    > Paul Lalli
    John, Dec 4, 2006
    #6
  7. John

    Paul Lalli Guest

    John wrote:
    > I did have a look for posting regulations before posting, but couldn't
    > find them.


    So you know how to search as well as you know how to program. They're
    posted here twice a week. They have the title "Posting Guidelines".
    This is not difficult.


    > I suppose that I should have looked harder. Your right a
    > short-but-complete program would be a more sensible and constructive
    > way of presenting the query. But in spite of all my errors the Todd W
    > managed to give me advice that solved my problem


    No, he gave you advice that got you *around* your problem. You clearly
    still don't know what a three-dimensional array is, or how to go about
    accessing the various levels of it. He gave you a piece of code that
    prints out debugging information. Possibly useful for your immediate
    needs, but worthless for any "real" programming.


    > quicker than you
    > managed to complain about my posts short-comings. So while you're
    > right to correct my posting style don't you think this group and the
    > world in general would be a nicer place with less unnecessary rude and
    > patronising behaviour?


    No no, see my post was *necessarily* rude, because so was yours. You
    are making the mistake of assuming I was trying to help you. When you
    post such a blatantly poor attempt to let us help you, why would I
    waste my time attempting to do that?

    > Also I'm 'sure' you'll be glad to know that it was a 3 dimensional
    > array that was printed out after I followed Todd W advice.


    No. It wasn't. You pretty clearly don't know what a "3 dimensional
    array" is. Or you lied about the error message. Or you lied about the
    actual code you posted. Either way, you're pretty clearly not worth
    helping.

    Yes. This was rude. No, I'm not sorry.

    Paul Lalli
    Paul Lalli, Dec 4, 2006
    #7
  8. Todd W <> wrote:
    >
    > "John" <> wrote in message
    > news:...
    >> Hello, just a little thing. I'm trying to print out a 3 dimensional
    >> array using the following code
    >>
    >> for(@AoAoA)
    >> {
    >> my @AoA =@{$_};
    >> for(@AoA)
    >> {
    >> my @array =@{$_};




    > What is probably happening is once you enter the the first inner loop, $_
    > from the outer loop is clobbered with the first array item from the inner
    > loop.



    That is not what is happening.

    foreach/for local()izes the loop control variable, there is no
    clobbering going on.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Dec 5, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alf P. Steinbach
    Replies:
    0
    Views:
    423
    Alf P. Steinbach
    Aug 18, 2003
  2. John Harrison
    Replies:
    4
    Views:
    6,914
    Default User
    Aug 19, 2003
  3. Icosahedron
    Replies:
    8
    Views:
    641
    Vivek
    Aug 21, 2003
  4. Venkat
    Replies:
    4
    Views:
    963
    Venkat
    Dec 5, 2003
  5. Wirianto Djunaidi
    Replies:
    2
    Views:
    195
    Wirianto Djunaidi
    Apr 29, 2008
Loading...

Share This Page