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

HTML in CGI radio_group 1

Status
Not open for further replies.

audiopro

Programmer
Apr 1, 2004
3,165
GB
I have a cgi radio button which works ok.
The values are correct but I want to add some HTML markup to the JOB_STATUS hash. If I just add formatting to the elements, the formatting is printed, even in speech marks.
Is there another way?
Code:
my %JOB_STATUS;
$JOB_STATUS{'C'}='Current explanation';
$JOB_STATUS{'A'}='Allocated explanation';
$JOB_STATUS{'F'}='Completed explanation';
$JOB_STATUS{'N'}='Not Allocated explanation';

print $query->radio_group(-name=>'jobstat',
	-values=>['C','A','F','N'],
	-default=>$FILE_INFO[10],
	-linebreak=>'true',
	-labels=>\%JOB_STATUS);

Keith
 
Per the CGI documentation, simply add an attributes parameter:


Secondly, you can simplify your code a little by following the design inspired in this thread:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]CGI::Pretty[/green] [red]qw([/red][purple] :html3 [/purple][red])[/red][red];[/red]

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

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$query[/blue] = new CGI::Pretty[red];[/red]

[black][b]my[/b][/black] [blue]@FILE_INFO[/blue] = [red]([/red][red]'[/red][purple]A[/purple][red]'[/red][red])[/red] x [fuchsia]12[/fuchsia][red];[/red] [gray][i]# Fake data[/i][/gray]

[black][b]my[/b][/black] [blue]@JOB_STATUS[/blue] = [maroon]hashKeys[/maroon][red]([/red][black][b]my[/b][/black] [blue]%JOB_STATUS[/blue] = [red]([/red]
	[purple]C[/purple]	=> [red]'[/red][purple]Current explanation[/purple][red]'[/red],
	[purple]A[/purple]	=> [red]'[/red][purple]Allocated explanation[/purple][red]'[/red],
	[purple]F[/purple]	=> [red]'[/red][purple]Completed explanation[/purple][red]'[/red],
	[purple]N[/purple]	=> [red]'[/red][purple]Not Allocated explanation[/purple][red]'[/red],
[red])[/red][red])[/red][red];[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [blue]$query[/blue]->[maroon]radio_group[/maroon][red]([/red]
	-[purple]name[/purple]		=> [red]'[/red][purple]jobstat[/purple][red]'[/red],
	-[url=http://perldoc.perl.org/functions/values.html][black][b]values[/b][/black][/url]		=> \[blue]@JOB_STATUS[/blue],
	-[purple]default[/purple]	=> [blue]$FILE_INFO[/blue][red][[/red][fuchsia]10[/fuchsia][red]][/red],
	-[purple]linebreak[/purple]	=> [red]'[/red][purple]true[/purple][red]'[/red],
	-[purple]labels[/purple]		=> \[blue]%JOB_STATUS[/blue],
[red])[/red][red];[/red]

[url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [maroon]hashKeys[/maroon] [red]{[/red]
	[url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Incorrect number of values[/purple][red]"[/red] [olive][b]if[/b][/olive] [blue]@_[/blue] [blue]%[/blue] [fuchsia]2[/fuchsia][red];[/red]
	[blue]@_[/blue] ? [red]([/red][url=http://perldoc.perl.org/functions/map.html][black][b]map[/b][/black][/url] [red]{[/red][blue]$_[/blue][red][[/red][fuchsia]2[/fuchsia][blue]*$_[/blue][red]][/red][red]}[/red] [red]([/red][fuchsia]0..[/fuchsia][red]([/red][blue]@_[/blue]/[fuchsia]2[/fuchsia]-[fuchsia]1[/fuchsia][red])[/red][red])[/red][red])[/red] : [red]([/red][red])[/red][red];[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]CGI::pretty - module to produce nicely formatted HTML code[/li]
[/ul]
[/tt]

With only 4 different values, it's not that big of a deal that you specify things in multiple places. But if you had any more than that, you'd definitely want to simplify your definitions to minimize the possibility of bugs due to mistypes.

- Miller
 
Miller,

what is this line doing?

@_ ? (map {$_[2*$_]} (0..(@_/2-1))) : ();

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
That line is part of the function that accepts an ordered hash as a parameter, and then returns the ordered keys of that hash. In functional words, it returns the even number indexes of an array.

Code:
[blue]@a[/blue] = [red]([/red][red]'[/red][purple]a[/purple][red]'[/red], [fuchsia]1[/fuchsia], [red]'[/red][purple]b[/purple][red]'[/red], [fuchsia]2[/fuchsia], [red]'[/red][purple]c[/purple][red]'[/red], [fuchsia]3[/fuchsia][red])[/red][red];[/red]
[blue]@b[/blue] = [maroon]hashKeys[/maroon][red]([/red][blue]@a[/blue][red])[/red][red];[/red] [gray][i]# Equals ('a', 'b', 'c');[/i][/gray]

[url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [maroon]hashKeys[/maroon] [red]{[/red]
	[url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Incorrect number of values[/purple][red]"[/red] [olive][b]if[/b][/olive] [blue]@_[/blue] [blue]%[/blue] [fuchsia]2[/fuchsia][red];[/red]
	[blue]@_[/blue] ? [red]([/red][url=http://perldoc.perl.org/functions/map.html][black][b]map[/b][/black][/url] [red]{[/red][blue]$_[/blue][red][[/red][fuchsia]2[/fuchsia][blue]*$_[/blue][red]][/red][red]}[/red] [red]([/red][fuchsia]0..[/fuchsia][red]([/red][blue]@_[/blue]/[fuchsia]2[/fuchsia]-[fuchsia]1[/fuchsia][red])[/red][red])[/red][red])[/red] : [red]([/red][red])[/red][red];[/red]
[red]}[/red]

It's only slightly obfuscated code given that the map function uses both the @_ and $_ variables causing some confusion when the array is dereferenced. However, I'm glad that you asked about this as I thought up a lot cleaner method for accomplishing the same thing.

Code:
[url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [maroon]hashKeys[/maroon] [red]{[/red]
	[url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Incorrect number of values[/purple][red]"[/red] [olive][b]if[/b][/olive] [blue]@_[/blue] [blue]%[/blue] [fuchsia]2[/fuchsia][red];[/red]
	[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$t[/blue] = [fuchsia]0[/fuchsia][red];[/red] [url=http://perldoc.perl.org/functions/grep.html][black][b]grep[/b][/black][/url] [red]{[/red][blue]$t[/blue] ^= [fuchsia]1[/fuchsia][red]}[/red] [blue]@_[/blue][red];[/red]
[red]}[/red]

The above not only accomplishes the exact same goal, but it also is more than 10 times faster when benchmarked.

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

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

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[olive][b]foreach[/b][/olive] [url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$n[/blue] [red]([/red][fuchsia]10[/fuchsia], [fuchsia]50[/fuchsia][red])[/red] [red]{[/red]
	
	[black][b]my[/b][/black] [blue]@a[/blue] = [red]([/red][fuchsia]1..[/fuchsia][blue]$n[/blue][red])[/red][red];[/red]
	
	[black][b]print[/b][/black] [red]"[/red][purple]array = [blue]$n[/blue] values[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
	[maroon]timethese[/maroon][red]([/red][fuchsia]1_000_000[/fuchsia], [red]{[/red]
		[purple]toggle[/purple] => [url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [red]{[/red]
			[black][b]my[/b][/black] [blue]$t[/blue] = [fuchsia]0[/fuchsia][red];[/red] [black][b]my[/b][/black] [blue]@b[/blue] = [url=http://perldoc.perl.org/functions/grep.html][black][b]grep[/b][/black][/url] [red]{[/red][blue]$t[/blue] ^= [fuchsia]1[/fuchsia][red]}[/red] [blue]@_[/blue][red];[/red]
		[red]}[/red],
		[purple]slice[/purple] => [black][b]sub[/b][/black] [red]{[/red]
			[black][b]my[/b][/black] [blue]@b[/blue] = [blue]@a[/blue] ? [red]([/red][url=http://perldoc.perl.org/functions/map.html][black][b]map[/b][/black][/url] [red]{[/red][blue]$a[/blue][red][[/red][fuchsia]2[/fuchsia][blue]*$_[/blue][red]][/red][red]}[/red] [red]([/red][fuchsia]0..[/fuchsia][red]([/red][blue]@a[/blue]/[fuchsia]2[/fuchsia]-[fuchsia]1[/fuchsia][red])[/red][red])[/red][red])[/red] : [red]([/red][red])[/red][red];[/red]
		[red]}[/red],
	[red]}[/red][red])[/red][red];[/red]
	[black][b]print[/b][/black] [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]
[red][[/red][red]/[/red][purple]CODE][/purple]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]Benchmark - benchmark running times of Perl code[/li]
[/ul]
[/tt]

And the results:

Code:
>perl scratch.pl

array = 10 values
Benchmark: timing 1000000 iterations of slice, toggle...
     slice: 19 wallclock secs (17.70 usr +  0.03 sys = 17.73 CPU) @ 56414.31/s (n=1000000)
    toggle:  1 wallclock secs ( 1.60 usr +  0.00 sys =  1.60 CPU) @ 624219.73/s (n=1000000)

array = 50 values
Benchmark: timing 1000000 iterations of slice, toggle...
     slice: 67 wallclock secs (61.59 usr +  0.00 sys = 61.59 CPU) @ 16236.93/s (n=1000000)
    toggle:  2 wallclock secs ( 1.58 usr + -0.01 sys =  1.57 CPU) @ 636132.32/s (n=1000000)

Anyway, the whole point of this function is to be able to declare a hash and an ordered array of its keys at the same time. This is often useful in a lot of html projects, given that the cgi modules do not typically accept an ordered hash as a parameter, but instead want a list of keys (which implies order), and the key value relationships seperately.

- Miller
 
OK, I think I understand now. [smile]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I also thought up another way using grep and array slices. I don't think there will be any method faster than the xor toggle, but I did a quick benchmark to confirm.

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

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

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@a[/blue] = [red]([/red][fuchsia]1..10[/fuchsia][red])[/red][red];[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][purple][b]\n[/b][/purple]array = 10 values[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[maroon]timethese[/maroon][red]([/red][fuchsia]1_000_000[/fuchsia], [red]{[/red]
	[purple]toggle[/purple] => [url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [red]{[/red]
		[black][b]my[/b][/black] [blue]$t[/blue] = [fuchsia]0[/fuchsia][red];[/red] [black][b]my[/b][/black] [blue]@b[/blue] = [url=http://perldoc.perl.org/functions/grep.html][black][b]grep[/b][/black][/url] [red]{[/red][blue]$t[/blue] ^= [fuchsia]1[/fuchsia][red]}[/red] [blue]@_[/blue][red];[/red]
	[red]}[/red],
	[purple]slice_w_map[/purple] => [black][b]sub[/b][/black] [red]{[/red]
		[black][b]my[/b][/black] [blue]@b[/blue] = [blue]@a[/blue] ? [red]([/red][url=http://perldoc.perl.org/functions/map.html][black][b]map[/b][/black][/url] [red]{[/red][blue]$a[/blue][red][[/red][fuchsia]2[/fuchsia][blue]*$_[/blue][red]][/red][red]}[/red] [red]([/red][fuchsia]0..[/fuchsia][red]([/red][blue]@a[/blue]/[fuchsia]2[/fuchsia]-[fuchsia]1[/fuchsia][red])[/red][red])[/red][red])[/red] : [red]([/red][red])[/red][red];[/red]
	[red]}[/red],
	[purple]slice_w_grep[/purple] => [black][b]sub[/b][/black] [red]{[/red]
		[black][b]my[/b][/black] [blue]@b[/blue] = [blue]@a[/blue] ? [red]([/red][blue]@a[/blue][red][[/red][black][b]grep[/b][/black] [red]{[/red]![red]([/red][blue]$_[/blue][blue]%[/blue][fuchsia]2[/fuchsia][red])[/red][red]}[/red] [red]([/red][fuchsia]0..[/fuchsia][blue]$#a[/blue][red])[/red][red]][/red][red])[/red] : [red]([/red][red])[/red][red];[/red]
	[red]}[/red],
	[purple]slice_w_grep2[/purple] => [black][b]sub[/b][/black] [red]{[/red]
		[black][b]my[/b][/black] [blue]@b[/blue] = [blue]@a[/blue] ? [red]([/red][blue]@a[/blue][red][[/red][black][b]grep[/b][/black] [red]{[/red][blue]$_[/blue] [maroon]&[/maroon] [fuchsia]1[/fuchsia] ^ [fuchsia]1[/fuchsia][red]}[/red] [red]([/red][fuchsia]0..[/fuchsia][blue]$#a[/blue][red])[/red][red]][/red][red])[/red] : [red]([/red][red])[/red][red];[/red]
	[red]}[/red],
	[purple]slice_w_grep2b[/purple] => [black][b]sub[/b][/black] [red]{[/red]
		[black][b]my[/b][/black] [blue]@b[/blue] = [blue]@a[/blue] ? [red]([/red][blue]@a[/blue][red][[/red][black][b]grep[/b][/black] [red]{[/red][blue]$_[/blue] [maroon]&[/maroon] [fuchsia]0[/fuchsia]b1 ^ [fuchsia]0[/fuchsia]b1[red]}[/red] [red]([/red][fuchsia]0..[/fuchsia][blue]$#a[/blue][red])[/red][red]][/red][red])[/red] : [red]([/red][red])[/red][red];[/red]
	[red]}[/red],
[red]}[/red][red])[/red][red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]Benchmark - benchmark running times of Perl code[/li]
[/ul]
[/tt]

And the results

Code:
C:\devel>perl scratch.pl

array = 10 values
Benchmark: timing 1000000 iterations of slice_w_grep, slice_w_grep2, slice_w_grep2b, slice
_w_map, toggle...
slice_w_grep: 21 wallclock secs (20.32 usr +  0.02 sys = 20.34 CPU) @ 49164.21/s (n=1000000)
slice_w_grep2: 22 wallclock secs (20.69 usr +  0.00 sys = 20.69 CPU) @ 48332.53/s (n=1000000)
slice_w_grep2b: 21 wallclock secs (20.48 usr +  0.00 sys = 20.48 CPU) @ 48828.12/s (n=1000000)
slice_w_map: 19 wallclock secs (17.56 usr +  0.02 sys = 17.58 CPU) @ 56866.65/s (n=1000000)
    toggle:  2 wallclock secs ( 1.54 usr +  0.00 sys =  1.54 CPU) @ 648929.27/s (n=1000000)

Ok, I think I can put this curiosity to rest. Unless there's anything else you would like made more clear? :)

- M
 
I'm afraid to ask [roll1]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hrm.... Let us examine this part of you that is afraid to ask. Always know that there are no bad parts, just good parts placed in bad roles. Is this feeling centered in any particular part of your body? Any tingling sensation or temperature gradation that you notice? Which chakra do you believe that this emotion is coming from?

Yes, lets explore this....

- Miller [yinyang]

 
The end is near. [wink]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Ahh, so you are not a yogi, but a follower of Kabbalah. Well, remember that of the 4 sons, it is the apathetic son that is considered the most evil. The Angry son, while not a believer, it still seated at the right hand of God because he is someone who is engaged, and could be turned to a wise son.

So ask your questions. It is through the question that we begin to attain enlightenment. Each new question beings us closer true understanding and the ultimate connection to the divine.

Ok, enough goofing off. Gotta to ready for my trip :)

- Miller
 
Ahh, so you are not a yogi, but a follower of Kabbalah.

No, I've just been watching "John from Cincinnati".

I've got my eys on you.

Have a good trip. [smile]

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

Part and Inventory Search

Sponsor

Back
Top