homepage Welcome to WebmasterWorld Guest from 54.211.34.105
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
Exploding Name=>Value type arrays in PERL
trillianjedi

WebmasterWorld Senior Member trillianjedi us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3821325 posted 7:46 pm on Jan 7, 2009 (gmt 0)

I knew this would happen. Having started to play around with PERL I'm getting quite into it :). Nice language - very strict (if requested), strongly typed. Forces good standards.

I'm stuck for resources though - I might be looking in the wrong place. Any good URL's I should bookmark ?

Current thing I'm trying to do is to explode some Name=>Value arrays into constituent parts so that I can pass them off to a URL as GET vars.

For example, I have a RAD_REQUEST var that contains numerous values which I can access like:-

$username = $RAD_REQUEST{'User-Name'}; #username="tj"
$password = $RAD_REQUEST{'Password'}; #password="me"

(if anyone is interested I'm playing with a RADIUS server)

I would like to explode those into a URL GET string that looks like:-

?User-Name=tj&Password=me

Which uses the original naming format from the array and with "=<value>" after it.

I found various array functions in PERL on my searching, but nothing for this type of 2D array.

Any pointers?

Many thanks.

 

trillianjedi

WebmasterWorld Senior Member trillianjedi us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3821325 posted 9:08 pm on Jan 7, 2009 (gmt 0)

Sorry, answering my own question again.

I found the "each" directive and built this:-

while ( ($key, $value) = each(%RAD_REQUEST) ) {
$url_get .= $key."=".$value."&";
}

$url_get = substr($url_get, 0, -1); #*

* I also found the Chop() directive, sounds cool (with a name like that I have to find a way to use it!).

krugs

5+ Year Member



 
Msg#: 3821325 posted 9:49 pm on Jan 7, 2009 (gmt 0)

chop() was replaced by chomp() for the most part. If you need to remove the end of line character/input record seperator, use chomp(). If you want to blindly remove the last thing on the end of a string or variable, use chop(),

krugs

5+ Year Member



 
Msg#: 3821325 posted 9:52 pm on Jan 7, 2009 (gmt 0)

Just for edification, what you are calling directives are functions, what you are calling a 2D structure is an associative array, more commonly referred to as a hash or hash table.

trillianjedi

WebmasterWorld Senior Member trillianjedi us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3821325 posted 10:12 pm on Jan 7, 2009 (gmt 0)

Yes, thanks for the edification :)

chop and chomp are interesting functions.

In this script I really need to remove all whitespace and newlines in the string, either side of it.

Is there a suitable "get rid of all whitespace and newlines" type function?

Thanks.

coopster

WebmasterWorld Administrator coopster us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3821325 posted 10:15 pm on Jan 7, 2009 (gmt 0)

You can use
map and join as well.
my $url_get = join("&", map { "$_=$RAD_REQUEST{$_}" } keys %RAD_REQUEST);

As they say, Tim Toady (TIMTOWTDI)

trillianjedi

WebmasterWorld Senior Member trillianjedi us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3821325 posted 11:19 pm on Jan 7, 2009 (gmt 0)

Coop - that's very slick ;)

Stuff like that is what makes me like a language.

Thanks! Implemented!

How might I clean up a string response that may contain both leading and trailing newlines and whitespace? I found:-

$string =~ s/\n//g;

... but as far as I can understand that would only replace newlines?

coopster

WebmasterWorld Administrator coopster us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3821325 posted 11:29 pm on Jan 7, 2009 (gmt 0)

\s is whitespace and includes newlines

Key_Master

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3821325 posted 11:48 pm on Jan 7, 2009 (gmt 0)

Try: $string =~ s/^[\s]*(.*?)[\s]*$/$1/;

krugs

5+ Year Member



 
Msg#: 3821325 posted 1:23 am on Jan 8, 2009 (gmt 0)

remove leading and trailing white space and any newline on the end:


$string =~ s/^\s*//;
$string =~ s/\s*$//;

On a side note:

[\s] is a needless use of the character class brackets []. \s is a character class, it doesn't need to be inside of the brackets unless combined with other things to make a character class that includes more than \s.

krugs

5+ Year Member



 
Msg#: 3821325 posted 1:30 am on Jan 8, 2009 (gmt 0)


I'm stuck for resources though - I might be looking in the wrong place. Any good URL's I should bookmark ?

[perldoc.perl.org...]

[perl.org...] (a little old but still relevant)

[hop.perl.plover.com...] (might be too advanced for perl newbies but its a great resource)

[perlmonks.com...] (tons of info, some is old but still relevant)

trillianjedi

WebmasterWorld Senior Member trillianjedi us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3821325 posted 10:18 am on Jan 8, 2009 (gmt 0)

remove leading and trailing white space and any newline on the end:

Thanks Krugs, although I also need to remove newlines at the beginning of any string.

Thanks for the links - will start some further reading.

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 3821325 posted 1:05 pm on Jan 8, 2009 (gmt 0)

i have "the camel book [oreilly.com]" within arms reach at all times.

it's more like a definitive guide than a learning book.
my "intro to perl" text was actually "CGI Programming with Perl".

there are also some pretty good manpages for perl and they are probably all available on perldoc.perl.org.
for example, check out man perltoot [perldoc.perl.org] (SEE ALSO [perldoc.perl.org]) for some light reading.

glad to see you're hooked!

Brett_Tabke

WebmasterWorld Administrator brett_tabke us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3821325 posted 3:08 pm on Jan 8, 2009 (gmt 0)

Best book I have is the Perl Cookbook (search for it). Some great stuff in there.

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3821325 posted 4:02 pm on Jan 8, 2009 (gmt 0)

my "intro to perl" text was actually "CGI Programming with Perl".

Do you mean "CGI Programming with Perl 5" by Eric Hermann? Ditto, still have it right here next to the Camel book. :-)

I think the end-of line character discussion reveals one of the most important fundamentals of perl, TMTOWTDI. Turning to the original question, here's another example of TMTOWTDI:


while ( ($key, $value) = each(%RAD_REQUEST) ) {
$url_get .= $key."=".$value."&";
}

You have to be very careful using while on variables stored in memory because in many cases, as long as the program is running the variable will exist, so while never returns false . . . get it? Use while for file or database reads, things you know will always have an ending, and use for or foreach on internal variables:

foreach $key (keys %RAD_REQUEST) {
$url_get .= $key."=".$RAD_REQUEST{$key}.'&amp;';
}
$url_get =~ s/\&amp\;$//;

<ahem> note the entity . . . programmers are always ragged for outputting crap code that won't validate, so output usable code . . . :-)

Or better yet, don't put the trailing &(amp;) there in the first place . . .


@keys = keys %RAD_REQUEST;
for $i (0..$#keys) {
$url_get .= $keys[$i].'='.$RAD_REQUEST{$keys[$i]};
if ($i < $#keys) { $url_get .= '&amp;'; }
}

An aside,

?User-Name=tj&Password=me

You do know what a terribly bad idea this is, right?

[edited by: rocknbil at 4:15 pm (utc) on Jan. 8, 2009]

trillianjedi

WebmasterWorld Senior Member trillianjedi us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3821325 posted 4:07 pm on Jan 8, 2009 (gmt 0)

You do know what a terribly bad idea this is, right?

I know what a terribly bad idea it would be if I did this in plain-text, if that's what you mean ;)

It's already on HTTPS and the password in this case is actually an MD5 hash, so it's fine. At least, on my understanding that HTTPS does actually encrypt GET vars and not just POST vars?

Thanks for the book links. Amazon just got paid again.

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 3821325 posted 11:30 pm on Jan 8, 2009 (gmt 0)

Do you mean "CGI Programming with Perl 5" ...?

no, this one:
CGI Programming with Perl, Second Edition By Scott Guelich, Shishir Gundavaram, Gunther Birznieks [oreilly.com]

krugs

5+ Year Member



 
Msg#: 3821325 posted 4:20 am on Jan 9, 2009 (gmt 0)

Thanks Krugs, although I also need to remove newlines at the beginning of any string.

Newlines at the beginning of a string is not too common but the regexps that were posted will also remove them as \n is part of the \s character class.

If you buy anything from amazon, get the Perl Bookshelf Reference on CD. It has several perl books pretty much for the price of one. Used copies are dirt cheap. My version 3 has:

Perl in a Nutshell, 2nd Edition (more of a reference and overview of many things perl)
By Nathan Patwardhan, Ellen Siever and Stephen Spainhour
Programming Perl, 3rd Edition (the infamous "Camel" book)
By Larry Wall, Tom Christiansen, and Jon Orwant
Learning Perl, 3rd Edition (the "Llama" book sometimes confused with the "Camel" book)
By Randal L. Schwartz and Tom Phoenix
Perl Cookbook (pretty much a crowd favorite)
By Tom Christiansen and Nathan Torkington
Perl and XML (a great resource)
By Erik T. Ray and Jason McIntosh
Perl and LWP (another great resource)
By Sean M. Burke
Mastering Perl/Tk (I don't do Tk myself)
By Steve Lidie and Nancy Walsh

Older versions have some different titles

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved