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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

editing a previous array entry

Status
Not open for further replies.

99mel

Programmer
Oct 18, 1999
379
0
0
GB
right... i'm trying to figure out the best way of editing a previous array entry.

the script basically reads each line from a flat file, with a number in the first field and populates the array @list. If i come across the same number in another line i.e. a duplicate (duplicates will ALWAYS be together as i have sorted the file), i skip this line. However i need to add some values from this line to the previous line (previous entry in the array)!!

Does that make any sense? hehe
 
It would be much easier if you use a hash instead of an array. Note that you can update an array value like this:

$array[32] = "new value";

 
Like this?

#!/usr/bin/perl

open (DATAFILE,"file.dat") || die "Can't open file\n";

while ($line = <DATAFILE>) {
chomp $line;
($number, $data) = split /\s+/,$line;
if ( $list[$#list] =~ /^$number/ ) {
$list[$#list] .= &quot; $data&quot;;
} else {
push @list, ($line);
}
}
close (DATAFILE);

foreach $xx (@list) {
print &quot;$xx\n&quot;;
}


Where the datafile looks like this:
1 cat
2 dog
3 beaver
3 ferret
4 possum
5 shrew
5 bat
7 dolphin
20 horse

and the output like this:
1 cat
2 dog
3 beaver ferret
4 possum
5 shrew bat
7 dolphin
20 horse
 
That looks like what i'm looking for! cheers

What excatly does this line do (i.e. is the $#list the previous array element?):

if ( $list[$#list] =~ /^$number/ ) {

Also... instead of adding (appending) the current elements data onto the last one (if the above condiition is true) i need to take the previous element value and mathmatically add the current value to the previous value!

Thanks again!
 
$#list gives the index of the last element of array @list, so the line:

if ( $list[$#list] =~ /^$number/ ) {

says use $#list to get the last element of @list using $list[$#list] and use the /^$number/ regex to check if it's the same starting number.

But, given the new information that the data needs to be summed, instead of appended, we can use raider2001's suggestion and make this much simpler. I'll do that in the next post.
 
Here's the new version...

open (DATAFILE,&quot;file2.dat&quot;) || die &quot;Can't open file\n&quot;;

while ($line = <DATAFILE>) {
chomp $line;
($number, $data) = split /\s+/,$line;
# This adds the value of $data to the existing hash entry
# in %list that has the key $number. If there is no
# existing entry, then one is created, with $data as
# it's value
$list{$number} += $data;
}
close (DATAFILE);

# the {$a <=> $b} after the sort is providing a numeric sort
# routine. Otherwise, the output data keys will be sorted
# as ASCII strings (eg. 1, 10, 2, 20, etc.)
foreach $xx (sort {$a <=> $b } keys %list) {
print &quot;$xx $list{$xx}\n&quot;;
}


Input data file:
1 10
2 20
3 30
3 40
4 50
5 60
5 70
7 80
20 90

Output data:
1 10
2 20
3 70
4 50
5 130
7 80
20 90


This more like what you were looking for?

Richard
 
One side benefit to the new version is that it no longer matters if the input data list is sorted, and the output will be sorted regardless of the input sort state.

Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top