Help with Multi-dimensional array 
Author Message
 Help with Multi-dimensional array

Hi all,

        There's something I would like to do, but I don't know how to do
it.  Basically, I'm creating a two dimensional array and would like to put
it into a one dimensional array (therefore creating a three dimensional
array).  In pseudocode, what I'm doing looks like this:

for ( ... ) {
  for ( ... ) {
    $_ = <INPUT_FILE>;


  }


  $i++;

Quote:
}

        My problem is that grid is always the same thing so biggrid ends
up being an array of grid's...all of which have the same rows in them.  In
C-speak (sorry, but I'm a C programmer ;) ), I need to re-malloc the grid
array so that biggrid stores a new memory address each time -- how do I do
it?

        Alternatively, if I could, on line 5 "push" directly into
$biggrid[$i], that would be great...I could remove the grid variable.

        Also, as you can tell, I'm still new to Perl.  I know pattern
matching, but don't know things like this or Packages, Objects, etc.  I
have the "Camel book" by O'Reilly, but truthfully, I find it hard to
understand.  Is there an O'Reilly book that one would suggest as a good
beginner book that's well written, well organized, and contains good
examples?  [BTW:  I said "O'Reilly" because I've read enough of them to
believe they're pretty good overall.]

        Thank you in advance!

Ray



Sat, 07 Jun 2003 20:00:53 GMT  
 Help with Multi-dimensional array

Quote:

> Hi all,

>    There's something I would like to do, but I don't know how to do
> it.  Basically, I'm creating a two dimensional array and would like to put
> it into a one dimensional array (therefore creating a three dimensional
> array).  In pseudocode, what I'm doing looks like this:

> for ( ... ) {
>   for ( ... ) {
>     $_ = <INPUT_FILE>;


>   }


>   $i++;
> }

>    My problem is that grid is always the same thing so biggrid ends
> up being an array of grid's...all of which have the same rows in them.  In
> C-speak (sorry, but I'm a C programmer ;) ), I need to re-malloc the grid
> array so that biggrid stores a new memory address each time -- how do I do
> it?

for ( ... ) {

  for ( ... ) {

  }

Quote:
}
>    Alternatively, if I could, on line 5 "push" directly into
> $biggrid[$i], that would be great...I could remove the grid variable.

for ( ... ) {
  for ( ... ) {

  }
  $i++;

Quote:
}

 Thomas


Sat, 07 Jun 2003 23:07:39 GMT  
 Help with Multi-dimensional array

Quote:

>Hi all,

>    There's something I would like to do, but I don't know how to do
>it.  Basically, I'm creating a two dimensional array and would like to put
>it into a one dimensional array (therefore creating a three dimensional
>array).  In pseudocode, what I'm doing looks like this:

>for ( ... ) {


Quote:
>  for ( ... ) {
>    $_ = <INPUT_FILE>;



You do not need the temp variable:


Quote:
>  }


>  $i++;
>}

>    My problem is that grid is always the same thing so biggrid ends
>up being an array of grid's...all of which have the same rows in them.
>    Alternatively, if I could, on line 5 "push" directly into
>$biggrid[$i], that would be great...I could remove the grid variable.


Quote:
>    Also, as you can tell, I'm still new to Perl.  

The minimum required knowledge can be obtained thus:

   perldoc perlreftut

Quote:
>I know pattern
>matching, but don't know things like this or Packages, Objects, etc.  I
>have the "Camel book" by O'Reilly, but truthfully, I find it hard to
>understand.  Is there an O'Reilly book that one would suggest as a good
>beginner book that's well written, well organized, and contains good

 ^^^^^^^^^^^^^

Beginner at what?

Beginner at programming in general, or beginner with Perl?

Quote:
>examples?  [BTW:  I said "O'Reilly" because I've read enough of them to
>believe they're pretty good overall.]

Beginner at programming in general:

  "Elements of Programming with Perl", Andrew Johnson (Manning Publications)

beginner with Perl:

  "Learning Perl", Randal Schwartz (O'Reilly)

disclaimer: I work for Manning, but I recommended Andrew's book even
            before that happened. I work for Randal, but I
            recommended his book before that happened too :-)

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Sat, 07 Jun 2003 22:16:04 GMT  
 Help with Multi-dimensional array

Right

Quote:

>   $i++;

Wrong.


by the time you got a few lines further on?


had forgotten this fact by the time you got a few lines further on?

Of course if you'd made appropriate use of my() you'd not have create
references to anonymous copies of the arrays in the fisrt place.

Rule of thumb: always declare all variables lexically scoped in
the smallest enclosing scope unless you have a reason not to.  This
applies not just in Perl but in all languages that have the concept of
lexically scoped variables (such as C).

Quote:
>    Alternatively, if I could, on line 5 "push" directly into
> $biggrid[$i], that would be great...I could remove the grid variable.

You can, but this would actually be _less_ efficinet and less Perlish.


--
     \\   ( )
  .  _\\__[oo

 .  l___\\
  # ll  l\\
 ###LL  LL\\



Sun, 08 Jun 2003 01:54:21 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. newbie help: sorting multidimensional arrays

2. help with multidimensional array....

3. help: multi-dimensional hash from flat array

4. Help with multi-dimensional array

5. multi-dimensional arrays - more help please!

6. multi-dimensional arrays with hashes help please

7. Help understanding Lists of Lists or Multi-dimensional arrays

8. Help: Multi-dimensional arrays & such

9. Need help with multi-dimensional arrays was:tie

10. arrays, multi-dimensional arrays, pointers and such

11. Associative arrays and multi-dimensional arrays.

12. Multi-Dimensional arrays, DBMs, and Assoc. Arrays

 

 
Powered by phpBB® Forum Software