2 dimensional array - Help needed 
Author Message
 2 dimensional array - Help needed

Hi all:

   I have an input file with the following format
1 2 3
4 5 6
7 8 9

   I wish to input it into an array.

   I am not sure how Perl do it. I need help on this.
Huang
Thanks in advance.



Sat, 23 Dec 2000 03:00:00 GMT  
 2 dimensional array - Help needed

Quote:
>   I have an input file with the following format
>1 2 3
>4 5 6
>7 8 9
>   I wish to input it into an array.
>The results I wish to accomplish is


those aren't the results that you want.  those are array slices -
somthing completely different (but it's not time for that yet).

how about multi-dimensional arrays, as discussed in the perldsc
man page?

   $matrix[$m][$n] = $value; #and so on.

good luck :)

--

CGI Meta FAQ <URL:http://computerdog.com/CGI_MetaFAQ.html>
Comprehensive Perl Archive Network (CPAN) <URL:http://www.perl.com>
Perl Mongers Travel Deals! <URL:http://www.pm.org/travel.html>



Sat, 23 Dec 2000 03:00:00 GMT  
 2 dimensional array - Help needed

Quote:


> >   I have an input file with the following format
> >1 2 3
> >4 5 6
> >7 8 9

> >   I wish to input it into an array.
> >The results I wish to accomplish is

> those aren't the results that you want.  those are array slices -
> somthing completely different (but it's not time for that yet).

> how about multi-dimensional arrays, as discussed in the perldsc
> man page?

>    $matrix[$m][$n] = $value; #and so on.

And, of course, there's always the perl4 hack using hashes:

        $x{1,1} = 1;
        ...
        $x{3,3} = 9;

This is somewhat slower than real 2-D arrays, but I keep finding that
it's fast enough for most purposes, and it's more readable.  You also
don't have to worry about making sure that the first-level vectors have
been allocated.  And for sparse arrays it often uses far less memory.



Sat, 23 Dec 2000 03:00:00 GMT  
 2 dimensional array - Help needed

Quote:

> I had the exact same problem.  Here's my solution for Perl 5.  
> (I'm new to Perl (3 days) so I'm not sure how clean/efficient
> this code is, but it seems to do the job...

You seem to be off to a good start.  You do a lot of things right:
checking the return value of open, using my variables, etc.

A few comments...

Quote:
>   for $i (1..$#usecol) {      # can skip $usecol[0]
>     $lastcol = $usecol[$i] > $lastcol ? $usecol[$i] : $lastcol;
>   }


  $lastcol = $col if $col > $lastcol;

Quote:
}
>   while ($line = <FILE>) {    # while line successfully read

while (defined($line = <FILE>)) {

Just in case the file ends with a zero on a line by itself and no
newline.  :-)

Quote:
>     $line = $line.$commentch; # trick: append $commentch so it
>                               # is always matched
>     $line=~/.*$commentch/;    # find $commentch
>     $line = $`;               # and keep everything before it

If the line already contained a comment, you have just obliterated the
contents of $line.  Consider: "foo # comment\n#" (with $commentch
already appended).  /.*$commentch/ will match 'foo #' at the beginning
of the string, so $` is the null string.

If you want to strip comments, try this:
s/$commentch.*//;

By the way, $` and kin should be avoided, as they slow down every regex
in the script.

You're actually returning the array itself, rather than a pointer.  (And
Perl has references instead of pointers.)  But you're probably just
mixing in C terminology.

Quote:
> return(1);  # indicates file included properly

1; # works too

--


    /                                  http://www.ziplink.net/~rjk/
        "It's funny 'cause it's true ... and vice versa."



Mon, 25 Dec 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Need Help with putting Hashes in a two-dimensional array

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

3. 2-dimensional array into 1 dimensional array?

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

5. help: multi-dimensional hash from flat array

6. Help with multi-dimensional array

7. 2 dimensional array suntax help, please

8. multi-dimensional arrays - more help please!

9. multi-dimensional arrays with hashes help please

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

11. Help: Multi-dimensional arrays & such

12. Help with Multi-dimensional array

 

 
Powered by phpBB® Forum Software