Welcome to WebmasterWorld Guest from 54.234.8.146

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Exploding Name=>Value type arrays in PERL

     

trillianjedi

7:46 pm on Jan 7, 2009 (gmt 0)

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



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

9:08 pm on Jan 7, 2009 (gmt 0)

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



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

9:49 pm on Jan 7, 2009 (gmt 0)

5+ Year Member



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

9:52 pm on Jan 7, 2009 (gmt 0)

5+ Year Member



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

10:12 pm on Jan 7, 2009 (gmt 0)

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



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

10:15 pm on Jan 7, 2009 (gmt 0)

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



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

11:19 pm on Jan 7, 2009 (gmt 0)

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



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

11:29 pm on Jan 7, 2009 (gmt 0)

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



\s is whitespace and includes newlines

Key_Master

11:48 pm on Jan 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

krugs

1:23 am on Jan 8, 2009 (gmt 0)

5+ Year Member



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

1:30 am on Jan 8, 2009 (gmt 0)

5+ Year Member




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

10:18 am on Jan 8, 2009 (gmt 0)

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



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

1:05 pm on Jan 8, 2009 (gmt 0)

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



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

3:08 pm on Jan 8, 2009 (gmt 0)

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



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

rocknbil

4:02 pm on Jan 8, 2009 (gmt 0)

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



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

4:07 pm on Jan 8, 2009 (gmt 0)

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



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

11:30 pm on Jan 8, 2009 (gmt 0)

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



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

4:20 am on Jan 9, 2009 (gmt 0)

5+ Year Member



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

 

Featured Threads

Hot Threads This Week

Hot Threads This Month