You might try something like this.
#!/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], " "; } }
(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,
sub rev { $len = length ($phr); $phr =~ s/\s+(w+)\s+(w+)/$2\s+$1; print "$phr\n"; }
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 initargs { $phr; }
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 ;)

In reply to Re: No grokkage on regex search/replace by Anonymous Monk
in thread No grokkage on regex search/replace by bluethundr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.