[Pdx-pm] array question
Tkil
tkil at scrye.com
Mon Apr 21 16:00:15 CDT 2003
>>>>> "Annett" == aspen <108" <aspen_108 at hotmail.com>> writes:
Annett> Here're some parts of the code where I'm running into the
Annett> problem. Be forewarned, I'm kind of a perl newbie...
That's ok, we're here to educate. Occasionally with a stick with
spiky bits on it, but really, we're just using tough love...
Annett> Here's the part where I'm reading the data into these arrays:
Annett> if ($get_temp == 1 && $inp_line !~ /Temperature/) {
Annett> @temp_array = split ' ', $inp_line;
Annett> for $temp_data (@temp_array) {
Annett> $temp[$seg][$lay] = $temp_data;
Annett> printf "%s%10.1f%10.1f%8.2f%8.2f%8.1f\n", $reg_date, $seg,
Annett> $lay, $temp[$seg][$lay], $tot_seg, $#{$temp[$seg]};
Annett> $lay++;
Annett> }
Annett> }
Without knowing where $seg is getting set (and is $lay getting reset
somewhere?), it's hard to figure out which values are being duplicated
or not.
Consider:
| #!/usr/bin/perl -w
|
| use strict;
|
| use Data::Dumper qw( Dumper );
| $Data::Dumper::Indent = 1;
|
| my @temp;
|
| while ( my $line = <DATA> )
| {
| next unless $line =~ /^Temperature/;
|
| my @vals = split ' ', $line;
| shift @vals; # get rid of label
|
| push @temp, \@vals;
| }
|
| print Dumper \@temp;
|
| exit 0;
|
| __DATA__
| Temperature: a b c
| Pressure: 1 2 3
| Temperature: alpha beta gamma
Which prints:
| $ ./annett2.plx
| $VAR1 = [
| [
| 'a',
| 'b',
| 'c'
| ],
| [
| 'alpha',
| 'beta',
| 'gamma'
| ]
| ];
Annett> Here's one example of how I'm trying to clear the array:
Annett> for ($seg = $#temp; $seg > 0; $seg--) {
Annett> for ($lay = $#{$temp[$seg]}; $lay > 0; $lay--) {
Annett> undef $temp[$seg][$lay];
Annett> }
Annett> }
You really should be able to just do:
undef @temp;
And this will blow away everything.
Remember, Perl is reference-counting and garbage-collecting; you
don't have to "undef" anything when you're done using it. (Exception:
circular structures.)
Annett> That print statement with the $#{$temp[$seg]} lets me look at
Annett> the index as it runs, and the array indices are not being
Annett> cleared with the undef. I gave your one suggestion
Annett> "@some_array = ();" a quick try, but that didn't clear the
Annett> indices either... not sure if there's a different syntax for a
Annett> 2-d array though.
I'm not quite clear what you mean by "clear the indices". If you
undef an array, or assign the empty list into it, then $#array should
be -1, and "scalar @array" should be 0. Consider:
| #!/usr/bin/perl -w
|
| use strict;
|
| my @a;
|
| @a = qw( a b c );
| print "orig: \@a = [ @a ], \$#a = $#a\n";
|
| undef @a;
| print "after undef: \@a = [ @a ], \$#a = $#a\n";
|
| @a = qw( a b c );
| print "refilled: \@a = [ @a ], \$#a = $#a\n";
|
| @a = ();
| print "after assign: \@a = [ @a ], \$#a = $#a\n";
|
| 0;
Which prints:
| $ ./annett1.plx
| orig: @a = [ a b c ], $#a = 2
| after undef: @a = [ ], $#a = -1
| refilled: @a = [ a b c ], $#a = 2
| after assign: @a = [ ], $#a = -1
So either "undef @array" or "@array = ()" will get rid of the indices
as advertised. Which is why I think there's a conversational gap
here.
Offhand, I'd guess that $seg and $lay aren't getting (re)set quite
right. Also, remember that Perl arrays are (almost) always counted
from 0, so if you set $array[100], then all the indexes from 0 to 99
inclusive are created and set to undef. If you want sparse arrays,
use hashes. :)
t.
More information about the Pdx-pm-list
mailing list