Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: 2d array

by CountZero (Bishop)
on Jan 03, 2013 at 20:52 UTC ( #1011517=note: print w/ replies, xml ) Need Help??


in reply to 2d array

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


Comment on Re: 2d array
Select or Download Code
Re^2: 2d array
by jnarayan81 (Initiate) on Jan 04, 2013 at 12:22 UTC

    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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2014-10-01 07:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (389 votes), past polls