Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

2D array in Perl

by Perl_ca_user (Initiate)
on Jun 20, 2013 at 19:25 UTC ( #1040015=perlquestion: print w/replies, xml ) Need Help??
Perl_ca_user has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys, I was trying to create in a 2-D array in my hash tag(well, it's kinda like a 3-D I guess).. When I tried to initialize it, I did something like:

my @array=(); $exonHash{$exonNumber}=\@array; #exonHash is the hashtag my @twoD_array0=(); my @twoD_array1=(); my @twoD_array2=(); my @twoD_array3=(); my @twoD_array4=(); $array[0]=\@twoD_array0; $array[1]=\@twoD_array1; $array[2]=\@twoD_array2; $array[3]=\@twoD_array3; $array[4]=\@twoD_array4;
and when I push stuff into the array, I did:
push(@{$exonHash{$exonNumber}}[1], $q1); push(@{$exonHash{$exonNumber}}[2], $med); push(@{$exonHash{$exonNumber}}[3], $q3); push(@{$exonHash{$exonNumber}}[4], $max);
It works in windows IDE, but out of no reason, after I upload it to linux, it gives error as Type of arg 1 to push must be array (not array slice) at line 86, near "$min)", which is saying the arrays are invalid. Do I have better ways to initialize arrays like this or did I make a mistake somewhere? Thank you very much.

Replies are listed 'Best First'.
Re: 2D array in Perl
by hdb (Monsignor) on Jun 20, 2013 at 19:57 UTC

    You do not need to initialize your structure at all due to Perl's autovivification: all intermediates are created automatically. The syntax below should work in both versions of Perl you are using.

    use strict; use warnings; my %exonHash; my $exonNumber = 1; my $med = 3; push @{ $exonHash{ $exonNumber }->[ 2 ] }, $med;
Re: 2D array in Perl
by choroba (Bishop) on Jun 20, 2013 at 19:37 UTC
    The reason is different version of Perl. In older versions of Perl, the first argument of push must be an array. Nowadays, you can use an array reference as well. The Windows box has the newer version, while the Linux one has a version that does not support the feature yet.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      I what is the correct syntax if I want to run it in Linux?

        what is the correct syntax if I want to run it in Linux?

        See the answer in Re: 2D array in Perl by Brother hdb.
        That one should do what you want with both perl versions and both operating systems.

        Cheers, Sören

        (hooked on the Perl Programming language)

Re: 2D array in Perl
by toolic (Bishop) on Jun 20, 2013 at 19:39 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1040015]
Approved by Old_Gray_Bear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2018-07-20 11:08 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (429 votes). Check out past polls.