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

2d array

by jnarayan81 (Initiate)
on Jan 03, 2013 at 20:17 UTC ( #1011511=perlquestion: print w/ replies, xml ) Need Help??
jnarayan81 has asked for the wisdom of the Perl Monks concerning the following question:

## Declare few single dimensional array my @row1 = qw(71 22 15 10 51); my @row2 = qw(91 82 28 11 91); my @row3 = qw(11 72 37 58 20); my @row4 = qw(21 42 63 24 16); my @row5 = qw(81 32 53 54 42); ## Adding all the single dimensional array reference into another arra +y my @array_2d = (\@row1, \@row2, \@row3, \@row4, \@row5); print "Print Using Array Index\n"; for(my $i = 0; $i <= $#array_2d; $i++){ # $#array_2d gives the highest index from the array for(my $j = 0; $j <= $#array_2d ; $j++){ print "$array_2d[$i][$j] "; } print "\n"; }

Is there any way to pass dynamic array values in my @array_2d i.e by using some loop ...

Thanks Jit

Sorry Monks, I din't explained my questions well. Below is the part of my code were I store the coordinates in @{"$speciesName"."_brk"}; Now every time I need to change the @matrix=\(@species,@chromosome,@brk_cordinates,@brk_decision,@chimp_brk,@dog_brk,@horse_brk,@human_brk,@monkey_brk,@pig_brk,@rat_brk); and undef at the end manually. I need to get rid of it. Note: @unique_species and @target_species is not fix, it can be anything.

for ($aaa=0; $aaa<=$#unique_species; $aaa++) # The @unique +_species is Chimp,Dog,Horse,Human,Monkey,Pig,Rat { @brk_species_name=split /\_/,$unique_species[$aaa]; my $speciesName= lc($brk_species_name[0]); @{"$speciesName"."_brk"}=0; foreach $cor(0..$#target_species) { if ("$target_species[$cor]" eq "$unique_species[$a +aa]") { #print "$target_species[$cor]\t$unique_species +[$aaa]\t$target_brk_cordinates[$cor]\n"; push @{"$speciesName"."_brk"},"$target_brk_co +rdinates[$cor]"; } } @{"$speciesName"."_brk"}=sortUniqueHash (@{"$speciesN +ame"."_brk"}); my $arrayEntries=checkNumber (@{"$speciesName"."_brk"} +); push (@allDecision, $arrayEntries); push (@allBreakpointCoordinates, @{"$speciesName"."_br +k"}); @{"$speciesName"."_brk"}=join (',',@{"$speciesName +"."_brk"}); ## it generate one string and enter in an array } # Chimp,Dog,Horse,Human,Monkey,Pig,Rat same order as s +ps file names in matrix .... Need to change ... @matrix=\(@species,@chromosome,@brk_cordinates,@brk_decisio +n,@chimp_brk,@dog_brk,@horse_brk,@human_brk,@monkey_brk,@pig_brk,@rat +_brk); #print "Print Using Array Index\n"; for(my $i = 0; $i <= $#matrix; $i++){ # $#array_2d gives the highest index from the array for(my $j = 0; $j <= $#matrix ; $j++){ print OUTFILE "$matrix[$i][$j] "; } print OUTFILE"\t"; } print OUTFILE "\n"; undef @target_species; undef @species; undef @chromosome;undef @matri +x; undef @brk_cordinates; undef @target_brk_cordinates; undef @da; + undef @brk_decision; undef @dog_brk; undef @horse_brk; undef @monkey_brk; undef @pig_brk; u +ndef @human_brk; undef @chimp_brk; undef @rat_brk; undef @mouse_brk; +undef @pig_brk; undef @dog_brk; undef @horse_brk; undef @cattle_brk; +undef @allBreakpointCoordinates; undef @all_sps_array; undef @allDecision; undef @matrix;

Comment on 2d array
Select or Download Code
Re: 2d array
by Anonymous Monk on Jan 03, 2013 at 20:36 UTC
    You don't need to declare your variable using separate variables and the cumbersome syntax you've used: you're just constructing an array of array-refs (or an array-ref to an array of array-refs). Start by reading perldoc perlref stem-to-stern.
Re: 2d array
by CountZero (Bishop) on Jan 03, 2013 at 20:52 UTC
    Try this:
    use Modern::Perl; use Data::Dump qw /dd/; my @array_2d; push @array_2d, do {chomp; [split ',']} while (<DATA>); dd(@array_2d); __DATA__ 71,22,15,10,51 91,82,28,11,91 11,72,37,58,20 21,42,63,24,16 81,32,53,54,42
    Printing your 2D array is done much easier, less error prone and more "perlish" as follows (if you do not want to use the Data::Dump or Data::Dumper functions):
    for my $row_ref (@array_2d) { for my $item (@$row_ref) { print "$item "; } print "\n"; }
    Actually your print routine contains an error: it only works because you have the same number of columns as rows! Do you spot your error?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

      Hi CountZero, Sorry I didn't explain my question well. I updated the question with script. Can you please suggest me how to resolve the issues. Thanks Jit

        It is difficult to give you a turn-key solution since we do not know the format of your input data, but perhaps you can get some inspiration from the following:
        use Modern::Perl; my @two_d_array; my %dictionary; sub add_data { chomp @_; my $type = shift; my $datapoints = shift; state $sequence; push @two_d_array, [ split ',', $datapoints ]; $dictionary{$type} = $sequence++; } while ( my $species = <DATA> ) { my $breaks = <DATA>; add_data( $species, $breaks ); } say "$_:\t@{ @two_d_array[ $dictionary{$_} ] }" for sort keys %diction +ary; __DATA__ Chimp 71,22,15,10,51 Dog 91,82,28,11,91 Horse 11,72,37,58,20 Human 21,42,63,24,16 Monkey 81,32,53,54,42 Pig 10,13,99,12,25 Rat 9,17,87,33,11
        The subroutine expects two parameters: the first is the name of the species and the second is the string of datapoints ("breaks" as you call them). It pushes the array_ref of the "breaks" on the @two_d_array and puts the name of the species as a key in the $dictionary hash with value the index of the corresponding "breaks" in the @two_d_array.

        As you can see in the output routine (line 20), we walk the $dictionary hash and pull out the corresponding "breaks" from the @two_d_array.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics
Re: 2d array
by BillKSmith (Chaplain) on Jan 04, 2013 at 04:34 UTC

    "Dynamic" is not a perl data type. I think that you mean that you want changes in the row arrays to appear in the matrix and changes made in the matrix to appear in corresponding row array. The code you posted does do this. If you wanted to initalize the matrix with the current value of the rows, but allow them to modified independently, you would use the [] operator rather than the \ operator.

    my @array_2d = ([@row1], [@row2], [@row3], [@row4], [@row5]);
    Bill

      Hi Bill, Thanks for the reply. I updated the question with some portion of my script. Can you please suggest me how to get rid of manual modification in script. Thanks Jit

        I do not understand your application well enough to offer a solution, but I do have several comments.

        Awkward perl code is usually a symptom of a poor choice of data structures. Your code makes extensive use of "symbolic reference." Most text books consider this very poor practice. (It is not even allowed under "use strict".) My first guess would be to change your matrix to hash-of-hashes.

        Bill

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2014-07-28 11:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (196 votes), past polls