Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: 2 dimensional array

by kabeldag (Hermit)
on Jun 23, 2008 at 05:43 UTC ( [id://693444]=note: print w/replies, xml ) Need Help??


in reply to 2 dimensional array

The "Use of unitialized value in concatenation... blah, blah" error may be because of the way that you are populating the array from the file. Or maybe it is because $myString isn't initialized before the concatenation and you are using warnings.

It would be good if you post the code you are using to populate the array from the file, and how you are referencing the array elements.

split may produce a leading undefined value, or a trailing undefined value depending on how you are trying to split the data in the file.

Please post the code that doesn't work.

Replies are listed 'Best First'.
Re^2: 2 dimensional array
by mountain_drew (Initiate) on Jun 23, 2008 at 06:38 UTC
    wow. didn't expect anyone to read this so late. i haven't had a chance to analyze your response but here is part of the code:
    $datafile = $ARGV[0]; chomp $datafile; open (INPUT, $datafile) || die ("Unable to open $datafile"); @data = <INPUT>; close INPUT; @dim = split(/\,/, $data[0]); $width = $dim[0]; $height = $dim[1]; @table = @data[1..$height]; for($row = 0; $row < $height; $row++) { for($col = 0; $col < $width; $col++) { $myString = $myString.$table[$row][$col]; } $myString =""; }
    and the data file i'm reading is:
    15,10 ADESFJRASLXDFRT QBRAINOUEWHGYED RIRURLKUNGEASDV NAOBXCSTACHUIOL OJKDGKJGHJUINHR AHRHOAIDFSETRGH RXANOGSYEROGATS TOUDOGSDSAVFTRY UORTUOFRHRJUIKO BTIARTHYEUVFGQA Dogs, Cats Train
    thanks
      Ok. Well @table = @data[1..$height]; may not be doing what you expect.
      If you are unsure about your data structures or are just curious, then you can use Data::Dumper
      use Data::Dumper; $datafile = 'testdata.txt'; chomp $datafile; open (INPUT, $datafile) || die ("Unable to open $datafile"); @data = <INPUT>; close INPUT; @dim = split(/\,/, $data[0]); $width = $dim[0]; $height = $dim[1]; @table = @data[1..$height]; print Dumper(@table); die; for($row = 0; $row < $height; $row++) { for($col = 0; $col < $width; $col++) { $myString = $myString.$table[$row][$col]; } $myString =""; }
      @table is not a multi-dimensional array (because of the way you tried to create it). Therefore, there is no such
      $table[$row][$col]. Each element of @data is a single line of $datafile. @table = @data[1..$height]; is merely creating a new array which contains the same elements as @data.

      I don't know exactly know what you are trying to do, but to get access to each byte of each element/row in @table, you can use substr(). You don't need a 2-dimensional array for that:

      use strict; use warnings; my $datafile = $ARGV[0] || die "File to open was not provided!"; my @data; open (DATA_FILE, '<', $datafile) || die ("Unable to open $datafile"); @data = <DATA_FILE>; close(DATA_FILE); my @dim = split /\,/, $data[0]; my $width = $dim[0]; my $height = $dim[1]; my @table; push (@table, @data[1..$height]); my $myString =''; for(my $row = 0; $row < $height; $row++) { for(my $col = 0; $col < $width; $col++) { $myString .= substr($table[$row], $col, 1); } $myString =""; }
        argh!! you're right. for the section i showed you i don't need a 2 dimensional array. i didn't include all the code. what i'm trying to do (for a job interview) is take the entire table and search it... "Return the number of times you find each search word in the data. Words go forwards, backwards, horizontal, vertical and diagonal." i'll try your suggestion. i know i'm in over my head. i'm about to give up. -thanks
        omg!!! that worked. thanks!!
      That code leaves table as the one-dimensional array:
      @table = ( "ADESFJRASLXDFRT\n" "QBRAINOUEWHGYED\n" "RIRURLKUNGEASDV\n" "NAOBXCSTACHUIOL\n" "OJKDGKJGHJUINHR\n" "AHRHOAIDFSETRGH\n" "RXANOGSYEROGATS\n" "TOUDOGSDSAVFTRY\n" "UORTUOFRHRJUIKO\n" "BTIARTHYEUVFGQA\n" );
      But then you access it as if it were a two-dimensional array. (Your code actually will try to use symbolic references; you should enable strict so that when that happens by accident, you are alerted to it.)

      So, you need to add the code that would turn the above into a two dimensional array. What is each column going to be?

      You probably want to chomp(@data) after reading it to get rid of the newlines.

        ok. i see what you're saying. i tried chomping the newlines after i created the table but that still doesn't work. can you suggest a way i can create the table so that it is a 2 dimensional array.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-19 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found