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

Perl CGI nested arrays to parameter

Status
Not open for further replies.

sjeps

Programmer
Jan 31, 2008
4
IT
Hi eveyone, i need a tip.

I am programming a perl page using cgi.

Now, i have a nested array (let's say @domains) and i know that each value can be accessed by $domains[x][y] and so on.

I want to pass this array by a hidden parameter like :
...
hidden(-name=>'domains',-value=>[@domains],-override=>1),
...

Then i read it with:
...
@domini=param('domini');
...

The problem is: the above example works with one dimensional arrays. If @domains is multi dimensional and i try to access to the fields of the parameter each field is empty.

The fields exist (i can test it with a foreach) but are empty.

What am i doing wrong? How should i change "-value=>[@domains]" ? Cause i am sure the problem is there.

Thanks a lot
 
To put it simply, CGI cannot work with complex data structures like that. You need to code an translation step that will save your array of arrays in a format that html supports. This will simply require multiple hidden fields. Something like the following:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]CGI[/green][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[gray][i]#...[/i][/gray]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@domains[/blue] = [red]([/red]
	[red][[/red][fuchsia]1..10[/fuchsia][red]][/red],
	[red][[/red][fuchsia]11..20[/fuchsia][red]][/red],
	[red][[/red][fuchsia]31..30[/fuchsia][red]][/red],
[red])[/red][red];[/red]


[gray][i]# Save Complex Array in Form[/i][/gray]

[blue]$htmlform[/blue] .= [maroon]hidden[/maroon][red]([/red]
	-[purple]name[/purple]     => [red]'[/red][purple]domains.index[/purple][red]'[/red],
	-[purple]value[/purple]    => [blue]$#domains[/blue],
	-[purple]override[/purple] => [fuchsia]1[/fuchsia],
[red])[/red][red];[/red]

[olive][b]for[/b][/olive] [black][b]my[/b][/black] [blue]$i[/blue] [red]([/red][fuchsia]0..[/fuchsia][blue]$#domains[/blue][red])[/red] [red]{[/red]
	[blue]$htmlform[/blue] .= [maroon]hidden[/maroon][red]([/red]
		-[purple]name[/purple]     => [red]'[/red][purple]domains.[/purple][red]'[/red] . [blue]$i[/blue],
		-[purple]value[/purple]    => [blue]$domains[/blue]->[red][[/red][blue]$i[/blue][red]][/red],
		-[purple]override[/purple] => [fuchsia]1[/fuchsia],
	[red])[/red][red];[/red]
[red]}[/red]

[gray][i]# Load Complex Array from form[/i][/gray]

[black][b]my[/b][/black] [blue]@domains[/blue] = [red]([/red][red])[/red][red];[/red]

[olive][b]for[/b][/olive] [black][b]my[/b][/black] [blue]$i[/blue] [red]([/red][fuchsia]0..[/fuchsia][maroon]param[/maroon][red]([/red][red]'[/red][purple]domains.index[/purple][red]'[/red][red])[/red][red])[/red] [red]{[/red]
	[black][b]my[/b][/black] [blue]@row[/blue] = [maroon]param[/maroon][red]([/red][red]'[/red][purple]domains.[/purple][red]'[/red].[blue]$i[/blue][red])[/red][red];[/red]
	[url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@domains[/blue], \[blue]@row[/blue][red];[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.10.0) Modules used :
[ul]
[li]CGI - Simple Common Gateway Interface Class[/li]
[/ul]
[/tt]

- Miller
 
How much data is in the @domians array? If it's not too much you could convert it all to a comma seperated string (or some other delimiter) and send it as one value in one hidden field and then split that string back into it's individual fields on the other end.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
thanks a lot MillerH, it worked.

for Kevin, well i'm getting things from a database so it might be a bit big string.

anyway it's strange cgi doesn't support multi dimensional arrays. how do you deal with some database rows with single dimensional arrays?
 
Because CGI and HTML are simple protocols. It's a rather typical programming challenge. Essentially you just need to serialize and deserialize the data so that cgi can process it. The method that I demonstrated is the manual way. You could also use some packages like Data::Dumper to output the array as a string that could be saved in a single variable and then eval'd later.

Since html doesn't support multi-dimensional arrays as a basic feature, there's no reason for CGI to support it as well. You don't get those until you reach javascript.

- Miller
 
There is a possibility you could use the Storable module 9core module), then you send the reference (whihc is just a string) to the data in the hidden variable and use Storable to freeze /thaw the data. I am not sure it will work, you would have to experiment.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top