Associative array of a two dimensional array 
Author Message
 Associative array of a two dimensional array

I am starting to go beyond my understanding of arrays. . .

I am making an associative array, each value of which contains a 2-D
array.  This is what it looks like


        open FILE, "$file";


again
        while (<FILE>) {

        }


        close FILE;

Quote:
}

(NOTE :  FILE contains multiple columns of data :
abc     123     234     324
fds     123     321     234
fdk     123     54      34
)


2-D array.  I don't really understand why.  Any insight would be
wonderful.  Thanks a million.

mike.

Sent via Deja.com http://www.*-*-*.com/
Share what you know. Learn what you don't.



Sat, 15 Dec 2001 03:00:00 GMT  
 Associative array of a two dimensional array

Quote:

> I am starting to go beyond my understanding of arrays. . .

> I am making an associative array, each value of which contains a 2-D
> array.  This is what it looks like


>    open FILE, "$file";


> again
>    while (<FILE>) {

>         }


>         close FILE;
> }
> (NOTE :  FILE contains multiple columns of data :
> abc        123     234     324
> fds        123     321     234
> fdk        123     54      34
> )

> later, I say

> 2-D array.  I don't really understand why.  Any insight would be
> wonderful.  Thanks a million.

> mike.

You can only store a scalar as a value to a hash key. You need to take a
reference to the array before you put it in the hash like so...


now to get that array back you can...


-- or --


references. A way to get around this is to do...



reference to it in $line{$temp}.

also see "perldoc perlref"


undef;

Just a thought that does not matter :-)

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.



Sat, 15 Dec 2001 03:00:00 GMT  
 Associative array of a two dimensional array


: undef;

small correction:



Andrew



Sat, 15 Dec 2001 03:00:00 GMT  
 Associative array of a two dimensional array
: I am starting to go beyond my understanding of arrays. . .

   You understand arrays just fine.

   It is "references" that you don't understand.

   They are explained in perlref.pod and perldsc.pod.

: I am making an associative array, each value of which contains a 2-D
: array.  

   You cannot do that in Perl.

   Perl does not _have_ multi-dimensional arrays.

   Perl *does* have arrays of references (possibly to another array),
   which is how you get the *effect* of a 2D array.

   This distinction is essential to understanding complex
   data structures in Perl.

: This is what it looks like


:       open FILE, "$file";
                   ^     ^

   You should always, yes *always*, check the return value from open():

      open FILE, $file or die "could not open '$file'  $!";

   Your double quotes serve no purpose other than making it harder
   to read.


   You have not shared the subroutine declaration for your splics()
   function.

   You have another syntax error there too.

   You are missing a semicolon.

   We are obviously not looking at the same code that you are looking at.

   Not much point in troubleshooting code that does not exist!

      Do not type in code!!!

      Use cut/paste!!!

      Else you'll just get a bunch of wise ass remarks about your
      typos instead of getting help with your real problem.

: again
:       while (<FILE>) {

:         }


                          ^^^

   Q: What context is that in?

   A: scalar context


   A: evaluates to the *number of elements* in the array

   You are storing integers as the values of your hash.

   You should be storing a reference to an array instead:


:         close FILE;
: }
: (NOTE :  FILE contains multiple columns of data :
: abc   123     234     324
: fds   123     321     234
: fdk   123     54      34
: )

   In Perl, we say that as below, then everybody understands (even perl!)
   and there is no ambiguity, plus others can run your code without
   creating other files.

while (<DATA>) {
..

__DATA__
abc   123     234     324
fds   123     321     234
fdk   123     54      34

: later, I say

: 2-D array.  

   Because Perl does not *have* 2D arrays.

   It has references to arrays...

: I don't really understand why.  

   ... so you have to *de* reference it.


: Any insight would be
: wonderful.  

   Seek insight in the files that are already installed on your
   hard disk.

--
    Tad McClellan                          SGML Consulting

    Fort Worth, Texas



Sat, 15 Dec 2001 03:00:00 GMT  
 Associative array of a two dimensional array

Quote:
> I am starting to go beyond my understanding of arrays. . .

> I am making an associative array, each value of which contains a 2-D
> array.  This is what it looks like

<snip>

Quote:
> later, I say

> 2-D array.  I don't really understand why.  Any insight would be
> wonderful.  Thanks a million.

Check out this example (pass the array as a reference)


print $x [ 0 ] [ 0 ];

#prints whizkid

--
Faisal Nasim (the Whiz Kid)
Web: http://wss.hypermart.net/
AOL: Whiz Swift  ICQ: 4265451
FAX: (815) 846-2877



Sun, 16 Dec 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Two-dimensional associative arrays?

2. Associative arrays and multi-dimensional arrays.

3. Extracting an array out of a multi-dimensional associative array

4. Weird Hash w/ n-dimensional two-dimensional array !!!!!!

5. 2-dimensional array into 1 dimensional array?

6. associative array or associative arrays

7. Associative array of associative arrays

8. Perl4: Associative multi-dimensional arrays?

9. Multi-dimensional Associative Arrays

10. How to manipulate 2 dimensional Associative arrays?

11. Initializing a 2-dimensional associative array

12. Perl4: Associative multi-dimensional arrays?

 

 
Powered by phpBB® Forum Software