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

How to access a hash vairable if in a while loop?

Status
Not open for further replies.

Tarianna

Programmer
Sep 4, 2010
16
US
Hello all,

I want to add to a hash, values read in after a sql sth read.

while (my @data_row = $sth->fetchrow_array){

#$row {'00001'} = {'ITEMDESC' => $data_row[1],
$row {"$data_row[0]"} = {'ITEMDESC' => $data_row[1],
'LISTPRCE' => FormatPrice($data_row[2]),
'ITEMQTY' => $data_row[3],
'IMAGEFILE' => FormatPath($data_row[0])};

}

If I use the commented $row assignment then everything is assigned like it should. But, if I use a variable like the '$row{"$data_row[0]"}...' assignment statement, it doesn't act like I expect it. How do I add to the parent hash? Hrmm..do I have to create the parent hash then add children hash elements to it?

Any help is appreciated.
-t
 
Nm! I found the answer...all i Had to do was remove the quotes (double or single) and it's interpreted and assigned the way I want it to. :) Yay!
 
Agh!

I was mistaken. Removing the quotes did not make any difference.

I want this as a result:

$row{'00001'} = {'ITEMDESC'=> $data_row[1], ... etc}

without having to hardcode the '00001' into the assignment statement like so:

$row{$data_row[0]} = {'ITEMDESC'=> $data_row[1], ... etc}

...I'm still stuck. When I removed the quotes then the parent's name became '$data_row[0]' so when I tried to query on itemnumber 00001 it did not return any results like so.

print $row{'00001'}->{'ITEMDESC'} yielded no values BUT

print $row{$data_row[0]}->{'ITEMDESC'} did even tho $data_row[0] contained no value.

Any help is appreciated.
-t (the hashed out kid)
 
This code works as expected for me, how does it differ from yours?

Code:
[gray]#!/usr/bin/perl -w[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]Data::Dumper[/green][red];[/red]

[url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [maroon]FormatPrice[/maroon] [red]{[/red] [url=http://perldoc.perl.org/functions/return.html][black][b]return[/b][/black][/url] [url=http://perldoc.perl.org/functions/sprintf.html][black][b]sprintf[/b][/black][/url][red]([/red][red]"[/red][purple][purple][b]\$[/b][/purple]%1.2f[/purple][red]"[/red], [url=http://perldoc.perl.org/functions/shift.html][black][b]shift[/b][/black][/url][red])[/red][red];[/red] [red]}[/red]

[black][b]sub[/b][/black] [maroon]FormatPath[/maroon] [red]{[/red] [black][b]return[/b][/black] [black][b]sprintf[/b][/black][red]([/red][red]"[/red][purple]/some/path/%s.png[/purple][red]"[/red], [black][b]shift[/b][/black][red])[/red][red];[/red] [red]}[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]%row[/blue][red];[/red]
[black][b]my[/b][/black] [blue]@data_row[/blue] = [red]([/red] [red]'[/red][purple]00002[/purple][red]'[/red], [red]'[/red][purple]desc[/purple][red]'[/red], [red]'[/red][purple]1.1[/purple][red]'[/red], [red]'[/red][purple]512[/purple][red]'[/red] [red])[/red][red];[/red]

[blue]$row[/blue] [red]{[/red][red]'[/red][purple]00001[/purple][red]'[/red][red]}[/red] = [red]{[/red]
        [red]'[/red][purple]ITEMDESC[/purple][red]'[/red] => [blue]$data_row[/blue][red][[/red][fuchsia]1[/fuchsia][red]][/red],
        [red]'[/red][purple]LISTPRCE[/purple][red]'[/red] => [maroon]FormatPrice[/maroon][red]([/red][blue]$data_row[/blue][red][[/red][fuchsia]2[/fuchsia][red]][/red][red])[/red],
        [red]'[/red][purple]ITEMQTY[/purple][red]'[/red] => [blue]$data_row[/blue][red][[/red][fuchsia]3[/fuchsia][red]][/red],
        [red]'[/red][purple]IMAGEFILE[/purple][red]'[/red] => [maroon]FormatPath[/maroon][red]([/red][blue]$data_row[/blue][red][[/red][fuchsia]0[/fuchsia][red]][/red][red])[/red]
[red]}[/red][red];[/red]

[blue]$row[/blue] [red]{[/red][red]"[/red][purple][blue]$data_row[/blue][0][/purple][red]"[/red][red]}[/red] = [red]{[/red]
        [red]'[/red][purple]ITEMDESC[/purple][red]'[/red] => [blue]$data_row[/blue][red][[/red][fuchsia]1[/fuchsia][red]][/red],
        [red]'[/red][purple]LISTPRCE[/purple][red]'[/red] => [maroon]FormatPrice[/maroon][red]([/red][blue]$data_row[/blue][red][[/red][fuchsia]2[/fuchsia][red]][/red][red])[/red],
        [red]'[/red][purple]ITEMQTY[/purple][red]'[/red] => [blue]$data_row[/blue][red][[/red][fuchsia]3[/fuchsia][red]][/red],
        [red]'[/red][purple]IMAGEFILE[/purple][red]'[/red] => [maroon]FormatPath[/maroon][red]([/red][blue]$data_row[/blue][red][[/red][fuchsia]0[/fuchsia][red]][/red][red])[/red]
[red]}[/red][red];[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] Dumper \[blue]%row[/blue][red];[/red]

Annihilannic.
 
I'm going to have to try it today in a .pl program.

It differs in this way: in my cgi script..when I type:

$debug2Template->param(s4=>"listprice: $row{'00001'}->{'LISTPRCE'}";

I'd expect to see the listprice that I just init'ed with $row with. But instead, it's blank. But if i replace '$row('00001')' with '$row($data_row[0])' then a value appears. Shouldn't a value be printed regardless of which way I reference it?

Hrmm..this and many more questions..coming your way. :p (sorry, I'm delirous today.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top