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 ref and pass an array of hash from HTML template to cgi script

Status
Not open for further replies.

Tarianna

Programmer
Sep 4, 2010
16
US
Hello all,

I'm fairly new to cgi scripting, html templates and perl. I am trying to sort an array of hash (which represents rows of a table) and having trouble with passing the data from html to cgi script (so I can sort). I know how to pass the array to the template but not vice versa.

Essentially the flow goes like this (a snippet and simplified):

1. Input a value, query the database, output a table (this is where I get stuck)

2. Problem: click on a sort submit button, which in turns triggers a cgi script...I am able to pass single variables to the triggered cgi script but I am stuck in passing the data(an array of hash values) that I want sorted to that cgi script. (POST if you want to know.)

In the beginning:
The query cgi declares array of hash like this:

pseudocode (i'mlazy so sue me :p)

First, query my database
while we fetch rows from the successful sql query
fill the hash
$row{myvalue} = $data_row[0]; #and so on
push the hash onto @rows
}
#set my template parm
$template->param(ROWS=>\@row);

THEN I display it then which is fine but then..say I want to pass @rows to another cgi script w/o having to requery the database again....this is where I'm mystified.

I've tried the following:
1. created a form where sort.cgi is called and I pass 2 values (that goes thru ok) but the array of hash (the stuff I need to sort!) doesn't.

I keep getting compiling errors (attempt to set parameter x with an array ref - parameter is not a TMPL_LOOP. and what was that I'm wondering...lol.

My cgi script that receives it from the template that displays the table:

my $q=new CGI;
my @data = $q->param('ROWS');

I want to sort the array then output it in a new sorted list (my intent is honorable I assure you. :p) I've been trying to find this answer for so long..that I'm delirious..lol.

So, in short, I'mlooking for a syntax or example how to set or access an array of hash from template to cgi script (not from script to template)..if anyone could help, I'd appreciate it.

Thank you much,
Tarianna - a very sleepy programmer
 
Hello Keith,

Thanks for your reply. :)

Yah, the pseudocode I provided is rather loose. I should have posted this comment with it: "assume all work code to be done across 3 to 4 cgi's and multiple displays after each results are achieved for each cgi." (ie. sort.cgi sorts an array of stuff and then displays, input.cgi allows input then validates..etc.)

My problem has turned into...how to sort an array of referenced hashes. :/ I will be delving into "how to reference a referenced hash."

For now, I've put this problem on the backburner but if anyone comes up with a solution, I'd love you long time. :)


 
What do you want to sort the array of hashes by? Hashes by definition contain multiple items of data, so... it's not self-evident. I'm guessing it's the data in one field of the hash which appears in each element in the array...?

Can you post an example of your data structure (with data obfuscated if necessary, to protect the innocent), perhaps using use Data::Dumper; print Dumper \@array so it is displayed in a convenient format, and then describe which components of the data you wish to use as a sort key?

Annihilannic.
 
I wish I could simply use Data::Dumper to display this array of referenced hashes but it's in a cgi script and that script gets info that's run by another cgi script...like so:

1. Input.cgi : asks for 10 or so values (user hits submit)
2. display.cgi: sql query done here and then displayed

User hits the "please sort on this column" button and that's where we're at. :) How i get data::dumper to show me anything 3 pages after when navigating thru..is more than this newbie can handle atm. (I love Data::Dumper..discovered it last night lol...we had a party!)

Debugging cgi scripts is a bit painful..tho I"ve gotten used to it. Here's a snippet of the structure of the array I'm trying to sort.

@array; (my array of records)
%record (my record of data consisting of:)
a) itemnumber
b) description
c) price
d) yadda yadda yah.

After assigning a value to record like so:

my %record;
$record{itemnmbr}='12345';
$record{description}='it's really really shiny and red';
$record{price}='.02';

we push that record (referenced to a hash) like this:

push @data,\%record;

So there ya have it..how do you sort by the record's fields? itemnmbr, description, price? I thought that

sort $a{$record}{$itemnmbr}<=>$b{$record}{$itemnmbr}

would do it but noooooo. I think it's cuz of the '\' in front of the record thingie when you push it to the @data array. (HIgh tech word huh? thingie!)

Again, the syntax for the sort command is probably wrong syntatically but it's not that I'm having problems with..it's how to reference the itemized fields of %record..the mystical referenced thingie (yah it is probalby female too.) :p

Ok, well, now to start a thread on including a require statement in a cgi script. :p

 
I don't understand how you can have multiple displays all at once. Might be worth taking a step back and concentrate on the code, one stage at a time. Pseudo code is all very well but it doesn't really help other people help you.

Keith
 
This sort syntax should work.

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/my.html][black][b]my[/b][/black][/url] [blue]@array[/blue][red];[/red]

[url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@array[/blue], [red]{[/red] [purple]itemnmbr[/purple] => [red]'[/red][purple]12345[/purple][red]'[/red], [purple]description[/purple] => [red]'[/red][purple]it[purple][b]\'[/b][/purple]s really really shiny and red[/purple][red]'[/red], [purple]price[/purple] => [red]'[/red][purple].02[/purple][red]'[/red] [red]}[/red][red];[/red]
[black][b]push[/b][/black] [blue]@array[/blue], [red]{[/red] [purple]itemnmbr[/purple] => [red]'[/red][purple]12346[/purple][red]'[/red], [purple]description[/purple] => [red]'[/red][purple]it[purple][b]\'[/b][/purple]s really really dull and blue[/purple][red]'[/red], [purple]price[/purple] => [red]'[/red][purple].01[/purple][red]'[/red] [red]}[/red][red];[/red]
[black][b]push[/b][/black] [blue]@array[/blue], [red]{[/red] [purple]itemnmbr[/purple] => [red]'[/red][purple]12344[/purple][red]'[/red], [purple]description[/purple] => [red]'[/red][purple]it[purple][b]\'[/b][/purple]s really really horribly pink[/purple][red]'[/red], [purple]price[/purple] => [red]'[/red][purple].10[/purple][red]'[/red] [red]}[/red][red];[/red]

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

[black][b]my[/b][/black] [blue]@sorted[/blue] = [url=http://perldoc.perl.org/functions/sort.html][black][b]sort[/b][/black][/url] [red]{[/red] [blue]$[/blue][red]{[/red][blue]$a[/blue][red]}[/red][red]{[/red]itemnmbr[red]}[/red] <=> [blue]$[/blue][red]{[/red][blue]$b[/blue][red]}[/red][red]{[/red]itemnmbr[red]}[/red] [red]}[/red] [blue]@array[/blue][red];[/red]

[black][b]print[/b][/black] Dumper \[blue]@sorted[/blue][red];[/red]

See perldoc perlreftut for more information about hash references and how to use them.

Annihilannic.
 
Thanks for taking the time to help me.

I've been thru several references and many tutorials for the last few days..I'm sure one more won't hurt me.

Ok, here it is..the code that inits the array of hashes.

<do the query sql statement here>

my @rows;
while (my @data_row = $sth->fetchrow_array){
my %row;
$row{ITEMNUM} = rtrim($data_row[0]);
$row{ITEMDESC} = $data_row[1];
$row{LISTPRCE} = FormatPrice($data_row[2]);

push @rows, \%row;
}

this data is shared amongst 3 diff cgi's and passed from inpug.cgi to form to displaying.cgi to form to the last part..the sort.cgi. (fyi) And here begins my question..how to access the ITEMNUM, DESC and PRCE?

Perhaps it is better to opt to have the SQL engine do the sort.
 
Nm! I just discovered dereferencing ($$)! wowee. ok, I have a lot to do today and when (and if) I'm able to get back to this subject, I'll use that in my sort command.

I believe that was the source of my problem..I didn't know how to DE-reference a referenced variable. :))

One problem down..2,899,993 more to go!

-t
 
I think you're creating a lot of work for yourself by doing it this way. You have a script that you effectively call like this to display your table:

display.cgi?id=10

It goes away, searches the database, and produces a table ordered in some default manner. All you need to do is add a new parameter to your script that gets it to show the data in a different order. Then, your column headings can link to addresses like this:

display.cgi?id=10&sort=date

I know this will make another hit on the database, but it's a lot less hassle than passing an array of values to and fro via cgi.

If database performance is a real problem, look at doing some kind of caching. But don't build some impossibly complex structure to avoid a single SELECT statement.

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
Ok, I think avoiding the complex multidimensional array is a good idea! It wasn't my code, it's currently in an existing program that I need to work around. o_O

The data is read into a structure like I described before and i figured out a way to put it into a simpler structure and yet not disturb the way it is processed (hard to explain, you're just gonna have to trust me.)

Thanks to all that replied to this question. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top