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!

array reference and printing help 2

Status
Not open for further replies.

tlogan

Programmer
Jun 26, 2001
33
US
I've read as many previous posts as I could stand and have not found a problem quite like this one. It seems easy enough to do, but I give up. A little help?

Here's the situation and problem...
I get data from my credit card service provider after a sale via "GET" and create an array of all the data passed (Thanks tsdragon...I lifted some of your code from a previous post!) like this:
Code:
# Split data into list (array) of key=value entries
@order_info = split(/&/, $buffer);
# Process the data passed in buffer through POST
foreach $x (0 .. $#order_info) {
# Convert plus signs back to spaces
$order_info[$x] =~ s/\+/ /g;
# Split into key and value (on the first equal sign found).
($key, $val) = split(/=/, $order_info[$x], 2);
# Convert %XX sequences into characters
$key =~ s/%(..)/pack("c", hex($1))/ge;
$val =~ s/%(..)/pack("c", hex($1))/ge;
# Replace list element with converted values
$order_info[$x] = $key . "=" . $val;
# Create associative array member
$order_info{$key} = $val;
}

The problem...I always get array elements named "quantitiy", "id" and "desc". I can print them like this, no problem:
Code:
print &quot;Product ID: $order_info{'id'}<br>\n&quot;;
print &quot;Quantity  : $order_info{'quantity'}<br>\n&quot;;
print &quot;Prod Description: $order_info{'desc'}<br>\n&quot;;

Now for the REAL problem...
When there are additional items ordered, the array elements are named &quot;quantity1&quot;, &quot;id1&quot; and &quot;desc1&quot;, etc. I CANNOT figure how to print out these additional elements after I print the one that I know and can hard-code. Here is what I've tried so far:
Code:
# Print any additional items ordered
foreach $x (1 .. $#order_info) {
print qq(Quantity: $order_info{'quantity$x'}<br> ID: order_info'id$x'}<br> 
DESC: $order_info{'desc$x'}<br>\n);
}

I've tried every combination of $x, [$x], {$x}, ($x) and even \$x I can think of and none of it prints out the additional items.
(side note -- I am aware that the above code will print a bunch of blank lines for elements that don't have a number in their name -- I'll fix that later).

Any help would be GREATLY appreciated.

Thanks,
Tom
 
$order_info{'quantity$x'}

Because of the single quotes it never interpolates.
Use double quotes.
$order_info{&quot;quantity$x&quot;}
 
yauncin -

Worked like a charm! I KNEW it had to be something simple.

THANKS!

Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top