0

This question may look like repetitive but I am confused with the concept. I searched this but couldn't get the answer. So finally I am posting this on forum

I am having a text file with data as

1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 1
3 4 5 6 7 8 9 1 2

I want to create array of array. Code i am writing for this is

#! /usr/bin/perl
#use strict;
#use warnings;

open (FH, "8.txt");
$i = 0;
while (<FH>) {
    @temp = split;
    push @AoA, [ @temp ];
    foreach (@temp) {
        push @{$AoA[$i]}, $_;
    }
    $i++;
}
print $$AoA[0][0];

I am not getting output. I know the shortest answer that is

push @$AoA, [ split ]

But I need the answer as above mentioned to understand the concept

9
  • 1
    Not sure what you want. You have extraneous code that does nothing useful. Once you remove the stuff you don't need, you end up with push @$AoA, [ split ];. Commented Sep 19, 2013 at 17:15
  • I may look silly but i am not getting the concept from push @$AoA, [ split ]; So i went long way. Commented Sep 19, 2013 at 17:16
  • 6
    Why do you tell Perl to silence your errors, then ask us what you errors are? Reinstate use strict; use warnings;. It's going to catch 6 or 7 errors in your code. Commented Sep 19, 2013 at 17:17
  • 3
    print $$AoA[0][0] does not refer to the @AoA array. If you had not commented out strict and warnings, you would not have made this mistake. Commented Sep 19, 2013 at 17:26
  • 2
    @Nitesh, turn strict and warnings back on. Fix the problems they tell you about, and you will probably have correct code, or at least something closer thereto. People will be more willing to help you if you make some effort to conform to generally accepted standards of good code, and that includes allowing the compiler to help you so we don't have to. Commented Sep 19, 2013 at 18:20

2 Answers 2

4

This line:

print $$AoA[0][0];

Gives no output, you say. That is because it refers to the variable $AoA. If you had not disabled use strict; use warnings; it would have given the following output:

Global symbol "$AoA" requires explicit package name at ...
Execution of script.pl aborted due to compilation errors.

Which is to say, @AoA and $AoA are two different variables, and you are trying to print an empty element. The reference you should make to get an element from @AoA would be

print $AoA[0][0];
Sign up to request clarification or add additional context in comments.

5 Comments

You're welcome. Put back use strict; use warnings;. It will help you very much in preventing problems like this.
And one more query. If we do this problem like while (<FH>) { push @$HoA, [ split ]; } print $$HoA[0][0] is working then.
I prefer the syntax $AoA->[0][0], which to me makes it clearer that it is an array ref. It also makes the dereferencing less ambiguous. But using @AoA and $AoA[0][0] seems the simplest IMO.
Can you explain that syntax push @$HoA, [ split]; .... What i think is we have declared array ref $HOA as $HOA = []; Then how values are going in different indexes. ... I am poor in writing. If u understood then please answer this as i am confused
split returns a list of your numbers, the brackets [ ... ] create an anonymous array ref, containing your numbers, and you push that onto your array ref. It is similar to doing @foo = split; push @$AoA, \@foo;.
2

You ask for a longer version of

push @AoA, [ split ];

by which I assume you mean

my @AoA;
while (<>) {
   push @AoA, [ split ];
}

However, you haven't presented any criteria which would define an acceptable length. for starters, there's

my @AoA;
while (<>) {
   push @AoA, [ split ];;
}

I suppose we could expand out [] to it's near-equivalent:

my @AoA;
while (<>) {
   push @AoA, do { my @anon = ( split ); \@anon };
}

Or if we split it across two lines:

my @AoA;
while (<>) {
   my @row = split;
   push @AoA, \@row;
}

3 Comments

I think i am not clear with my question. This part is clear. The other part in forwach loop is not working
There is no other part. That's it completely.
Framing my question again. My code is not working as expected. Can you tell me what is wrong in the code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.