Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PROBLEM in working with 2-dimensional array

Status
Not open for further replies.

theSeeker03

Programmer
Jan 16, 2004
17
US
I am reading pipe-delimited data, line by line, from file INUSERS and storing it in a 2-dimensional array. However, when I print out the results, only the data for the last line of the file is being printed. AND it is being printed as many times as there are lines present in the input file!
How can I change it to print evey line? The i and j are iterating as expected and as i step through when debugging, the correct lines are fed in.

=================

my $numOfEntries =0;

while (<INUSERS>){
chomp($_);
@line = split(/\|/,$_);
$arrayOfEntries[$numOfEntries]= \@line;
$numOfEntries++;
}

for ($i=0;$i<$numOfEntries;$i++){
for ($j=0;$j<$#{$arrayOfEntries[$i]}+1;$j++){
print OUTUSERS $i." ".$j." ".$arrayOfEntries[$i]->[$j]."\n";
}
}
 
I'm sure there's a cleaner way to do this, but it seems to work:
Code:
while (<INUSERS>){
      chomp($_);
      $arrayOfEntries[$numOfEntries]= ([split(/\|/,$_)]);
      $numOfEntries++;
}
I think @line was getting overwritten on each pass, so what I think this does is it creates a reference to an anonymous array.. I just started playing around with data structures, so someone correct me if I'm wrong.
 
chazoid's exactly right about @line. When you don't [tt]use strict[/tt] all variables are global. All references point to the same array which you repopulate every time. If you'd throw a [tt]my[/tt] in front of @lines inside the loop, it should localize it and make a new one on each pass.

Still, I think the anonymous arrays are the more perlish way to do it. You also don't have to keep track of $numOfEntries if you just push all the values on. Just evaluate @arrayOfEntries in scalar context and it'll give the information to you.
Code:
my @arrayOfEntries;
while(<INUSERS>) {
	chomp;
	push @arrayOfEntries, [ split /\|/ ];
}

for (my $i=0; $i<@arrayOfEntries; $i++) {
    for (my $j=0; $j<@{$arrayOfEntries[$i]}; $j++) {
        print OUTUSERS "$i $j $arrayOfEntries[$i][$j]\n";
    }
}
Your code makes me think you're a C programmer. If that's the case, then you should appreciate a little predeclaration. :) Having [tt]use strict[/tt] is especially important when dealing with references and data structures.

________________________________________
Andrew - Perl Monkey
 
Thank you, icrf and chazoid. And yes, icrf, you are correct: most of my programming has been with C.
 
An even more perlish way would be to use an anonymous hash;

my $hasref = {};

while(<INUSERS>) {

(my $first, my $last) = split(/|/);

$hasref{$first} = $last;

}

then to print;

foreach (keys(%$hashref)) {

print $_ . " : " . $hashref{$_} . "\n";

}

or alphabetically;

foreach (sort {$a cmp $b } keys(%$hashref)) {

print $_ . " : " . $hashref{$_} . "\n";

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top