in reply to No grokkage on regex search/replace
(Oh, and my sentence brings up a useful discussion of what do you consider a word ... I doubt the problem specifies it exactly, but would you want 'alley-way' to be a single word, or would you want it to be reversed to way-alley or some other variant? It changes what approach you take--albeit a simple change in this case. Also, regexes aren't always the answer. They can be *extremely* powerful and useful ... but don't use a nuke to kill a fly when you've got a perfectly good flyswatter.) For information on the 'split' function you might try here. For info on using strict, I recommend searching around the monastary ... and if your book doesn't have it, well, ditch it! And as said before, be very wary of doing anything in CGI without first knowing a heck of a lot about security ... it doesn't hurt to emphasize it again, so make sure you research some on this. Again, this site is a good resource, but a decent cgi book would be another resource. But first things first,#!/usr/bin/perl use strict; use warnings; use diagnostics; PrintInRevWordOrder ("The tall brown cat walked down the alley-way."); sub PrintInRevWordOrder { my @string = split /\s+/, shift; # the problem says this is to be passed as an argument # but you are using global variable. bad practice in general. # C-stlye for loop ... usually I use more perl-esque structures, # but hey -- TMTOWTDI (there's more than one way to do it!) for (my $i = $#string; $i > 0; $i--) { print $string[$i], " "; } }
WHy do you need the length? You don't use it anywhere else. And you seem to again be using global variable, despite previous comments about them. That leads me to believe you don't understand, rather than that you are ignoring the comments about them. Again, this site is a good resource, the web in general or a decent programming book. It gets into topics like "namespace" and object-oriented design principles such as encapsulation ... so if you've never heard of any of this before, search around or ask and you're likely to get an answer.sub rev { $len = length ($phr); $phr =~ s/\s+(w+)\s+(w+)/$2\s+$1; print "$phr\n"; }
What? This is *not necessary*. In perl, you do not need to intialize your variable. It is, though, good practice to declare them. But, again, this is a global variable. At the start of your sub, you might say "my $phr" but this here does nothing useful whatsoever. Look up what 'my' does in your book. If it doesn't talk about it, further reason to get the Camel. It's starting to look like you'll just be spending more of your time re-learning bad practices if you don't get it soon ;)sub initargs { $phr; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: No grokkage on regex search/replace
by Anonymous Monk on May 23, 2004 at 19:12 UTC | |
|
Re: Re: No grokkage on regex search/replace
by TomDLux (Vicar) on May 23, 2004 at 22:10 UTC |