
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."