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

counting recurring items in a DB

Status
Not open for further replies.

ibjdt

Programmer
Nov 25, 2002
63
i have a flat file with this configuration
customerID::itemCode::month.yr::#ofSearches

the #ofSearches is a running total for each itemCode - by customerID - in the correpsonding month.yr

in an admin script i try to extract each itemCode with the total #ofSearches in the current month (for all customers -- in later admin screens i break it down specifically by customer)

but i get the error
Modification of a read-only value

related to the way i try to add the #ofSearches:

Code:
open (BPNSEARCH, "data/Bpnsearch.txt");
@bpnsearch = <BPNSEARCH>;
close (BPNSEARCH);
foreach $bpnsearch (@bpnsearch)
{
 	chomp ($bpnsearch);
	@bpnsearch2=split(/::/,$bpnsearch);

######THIS IS WHERE I GET THE ERROR
#CREATE VAR OF EACH ITEMCODE AND INCREMENT BY #OF SEARCHES
	${$bpnsearch2[1]} += $bpnsearch2[3];
#####

foreach $bpnsearch (@bpnsearch)
{
 	chomp ($bpnsearch);
	@bpnsearch2=split(/::/,$bpnsearch);
#STORE EACH INCREMENT FROM ABOVE IN TEMP FILE FOR USE LATER
	unless($i{$bpnsearch2[1]}++)
	{
		push(@Tbpnsearch, "${$bpnsearch2[1]}\::$bpnsearch2[1]\n");
	}
}

any suggestions are greatly appreciated.

thanks.

 
Instead of trying to create a var, create a hash [tt]$itemcode{$bpnsearch2[1]} += $bpnsearch2[3];[/tt]
The second loop (BTW the first one is unterminated) is obscure to me (and why redo the splits?): try to rewrite it using the hash as above in a way that does what you want and come back if you still have problems.


Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
${$bpnsearch2[1]} += $bpnsearch2[3];

probably needs to be

$myhash{$bpnsearch2[1]} += $bpnsearch2[3];

Then modify the rest of the code to match.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
yeah.. what prex said :)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Put some print statements in. My guess is that your var: $bpnsearch2[1] is evaluating to something like ARG. This would cause
Code:
${$bpnsearch2[1]} += $bpnsearch2[3];
# to evaluate as
$ARG += $bpnsearch2[3];
In general the above code would be risky. If you had no control over the values in $bpnsearch2[1], it could potentially evaluate into any variable you're using. Try using a %hash_var. eg:
Code:
READ_LOOP: {
   $itemCode{$bpnsearch2[1]} += $bpnsearch2[3];
}
REVIEW_LOOP:
{
   foreach $key (sort keys %itemCode) {
      print "key>$key<  val>$itemCode{$key}<\n";
   }
}
This will make $bpnsearch2[1] references into a hash versus defining them as variables on the fly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top