Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

reading input into a number of arrays

by Dannypje (Initiate)
on Feb 25, 2020 at 15:57 UTC ( [id://11113410]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

probably the code below is not the best way to do it, but it's something I understand (or at least I thought I understood). Intention is to read 3 lines of characters (say 1111111, 2222222, 3333333) and store them in a 2 dimensional array $a(1,1) through $a(3,7) (I know, I know, I should start from 0, but I don't think that's the issue).
The thing is, when I print the array inside the loop, I nicely get 1111111, 2222222, 3333333 as output. However, when I try to print outside the loop, I get 3 times my last input (3333333, 3333333, 3333333).
I don't understand how this happens. Please enlighten me.
TIA
Please note, the square brackets around the indices were lost in translation somewhere, the syntax is hence not the problem.

for ($i=1;$i<=3;$i++) { $ingang=<>; chomp $ingang; ($a[$i,1],$a[$i,2],$a[$i,3],$a[$i,4],$a[$i,5],$a[$i,6],$a[$i,7])=spl +it('',$ingang); } print "Resultaat\n"; for ($p=1;$p<=3;$p++) { for ($j=1;$j<=7;$j++) { print $a[$p,$j]; } print "\n"; }

Node formatting cleaned up by GrandFather

Replies are listed 'Best First'.
Re: reading input into a number of arrays
by choroba (Cardinal) on Feb 25, 2020 at 16:07 UTC
    Perl doesn't support the comma separated indices for multidimensional arrays. Instead of
    $a[$i, 1]

    use

    $a[$i][1]

    Update: Use warnings, Perl will tell you:

    Multidimensional syntax $a[$i,1] not supported at ...

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      You can emulate multi-dimentional arrays with hashes though. It's a feature not many people use.

      my %multidim; my $x = 3; my $y = 4; $multidim{ $x, $y } = 42; print $multidim{ $x, $y }, "\n";
      Choroba, thanks! I knew this. My shame is infinite.
Re: reading input into a number of arrays
by johngg (Canon) on Feb 25, 2020 at 16:30 UTC

    A simpler way to populate your AoA avoiding C-style loops.

    use strict; use warnings; use Data::Dumper; open my $inFH, q{<}, \ <<__EOD__ or die $!; 1111111 2222222 3333333 __EOD__ my @AoA; my $x = 0; while ( <$inFH> ) { chomp; $AoA[ $x ++ ] = [ undef, split m{} ]; } close $inFH or die $!; print Data::Dumper->Dumpxs( [ \ @AoA ], [ qw{ *AoA } ] );

    The output.

    @AoA = ( [ undef, '1', '1', '1', '1', '1', '1', '1' ], [ undef, '2', '2', '2', '2', '2', '2', '2' ], [ undef, '3', '3', '3', '3', '3', '3', '3' ] );

    I hope this is useful.

    Update: Even simpler would be to use push and avoid the counter entirely.

    use strict; use warnings; use Data::Dumper; open my $inFH, q{<}, \ <<__EOD__ or die $!; 1111111 2222222 3333333 __EOD__ my @AoA; while ( <$inFH> ) { chomp; push @AoA, [ undef, split m{} ]; } close $inFH or die $!; print Data::Dumper->Dumpxs( [ \ @AoA ], [ qw{ *AoA } ] );

    Cheers,

    JohnGG

Re: reading input into a number of arrays
by BillKSmith (Monsignor) on Feb 26, 2020 at 22:46 UTC
    Strictly speaking, Perl does not support multidimensional arrays. There are at least two ways that they can be simulated. tobyink has demonstrated the hash method documented in perldata. The other is documented in the "Arrays of Arrays" section of perldsc. The first has the advantage that the source code looks more the way we expect. The other, works the way we come to expect Perl to work. Here is a solution to your problem using slices (only available in the second method ref perldata).
    use strict; use warnings; my $infile = \do{my $chars = "1111111\n2222222\n3333333\n"}; open my $FH, '<', $infile or die "Cannot open input"; my @a; for my $i (1..3) { my $ingang = <$FH>; chomp $ingang; $a[$i] = [undef, split //, $ingang]; } for my $p (1..3) { print @{$a[$p]}[1..7], "\n"; }
    OUTPUT: 1111111 2222222 3333333
    Bill
Re: reading input into a number of arrays
by Anonymous Monk on Feb 26, 2020 at 15:57 UTC

    As for why the original code did what it did:

    It looks like Perl parsed the comma-delimited list of subscripts as a Perl list, which it then evaluated in scalar context to get the subscript. When a list is evaluated in scalar context it yields its last element. So $a[$i,1] was seen as $a[1].

    All of this would probably have puzzled you much less if you had started your script with

    use strict; use warnings;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11113410]
Approved by Corion
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found