Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Splitting multiple commands with regex failure

by CalebH (Novice)
on Aug 24, 2012 at 07:09 UTC ( #989437=perlquestion: print w/ replies, xml ) Need Help??
CalebH has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I have been working on a rather basic script in Perl (using the CGI.pm module), and am combining it with Ajax. The problem is in my parsing abilities, which I am trying to handle completely in Perl, rather than in the javascript form validation with my Ajax. The Code is have so far is as follows:
use CGI qw(param); $command = param("command"); $command =~ m/(!\w+\s?.+?)(?=!\w)/simg; if ($command eq "") { print "Add a command, man!\n"; } if $command ne "") { if $command =~ /^\!Book/) { print "Found a Book\n\n"; } elsif ($command =~ /^\!Number/) { print "Time to log a phone number\n\n"; } print "Command has the following in it : $command and was started at \ +n"; printf "Time %s\n", scalar(localtime); print "Command 1 is $1\n"; print "And Command 2 is $2\n\n"; print "Command 3 is $3\n\n"; print "\n"; } # (!\w+\s?)(.+?(?=!\w)) is the original regex I was using

The purpose of the script is to take a lot of input through a textarea, and split it based on various 'commands', all seperated by a !Command syntax. An example above is !book, but I also have !number and a few others, and various actions need to be performed based on the command type.

I am aware that I am missing the shebang line, but at the moment I am just running this script locally on my Windows machine and it's working as is, just not spitting out the results I want (It parses the first parameters, like "command=!book this is my test book !number 1222223 !book another book" will just split out "!book this is my test book", and ignore the rest.

What I would like to happen is to have the 'commands' pushed into an empty array, and use something such as Getopt::Param to parse them, or even getopt::long.

And as for why I am not using those to start with; I just wanted a simple way to do everything using one textarea and one param, and split it into multiples from that.

Thanks Monks, and apologies for the long winded first post!

Caleb

Comment on Splitting multiple commands with regex failure
Download Code
Re: Splitting multiple commands with regex failure
by 2teez (Priest) on Aug 24, 2012 at 07:43 UTC
    Hi,

    To start with check this line:

    if $command ne "") { ## Oops ... ### should be if ($command ne "") { ...
    ..(It parses the first parameters, like "command=!book this is my test book !number 1222223 !book another book" will just split out "!book this is my test book", and ignore the rest.

    If you want just !book this is my test book, then you might have to check your regex again!!!
    Beacuse this: m/(!\w+\s?.+?)(?=!\w)/simg; was just doing it job. Really.

    Please also check this: # (!\w+\s?)(.+?(?=!\w)) is the original regex I was using
    That was no where in your code as indicated in the original post. so this print "Command 3 is $3\n\n"; will cry foul!!! As this m/(!\w+\s?.+?)(?=!\w)/simg; is not equal to m/(!\w+\s?)(.+?(?=!\w))/;
    Knowing that your data is unknown.

Reaped: Re: Splitting multiple commands with regex failure
by NodeReaper (Curate) on Aug 24, 2012 at 07:46 UTC
Re: Splitting multiple commands with regex failure
by moritz (Cardinal) on Aug 24, 2012 at 07:48 UTC

    For splitting the commands, you can split before a ! sign

    use strict; use warnings; my $command = '!book this is my test book !number 1222223 !book anothe +r book"'; my @commands = split /(?=!)/, $command;

    Then if you want to ignore all but the first command, just use $commands[0]

    This leaves trailing spaces in the commands. If you want to remove those too, split on /\s*(?=!)/ instead.

Re: Splitting multiple commands with regex failure
by davido (Archbishop) on Aug 24, 2012 at 07:51 UTC

    Let's remove for a moment the additional confusion that CGI introduces. And then let's fix those syntax errors that are preventing your code from even compiling. Now we can look at your script like this:

    use strict; use warnings; my $command = do{ local $/ = undef; <DATA> }; $command =~ m/(!\w+\s?.+?)(?=!\w)/simg; if( $command eq "" ) { print "Add a command, man!\n"; } else { if( $command =~ /^\!Book/ ) { print "Found a Book\n\n"; } elsif ( $command =~ /^\!Number/ ) { print "Time to log a phone number\n\n"; } print "Command has the following in it : $command and was started + at \n"; printf "Time %s\n", scalar(localtime); print "Command 1 is $1\n"; print "And Command 2 is $2\n\n"; print "Command 3 is $3\n\n"; print "\n"; } __DATA__

    Now what input should we be considering? What exactly would go under "__DATA__"? And then next, tell us what exact output you want to see (wrap that in a separate set of code tags).

    Once we know those two variables, we'll have a better chance of being able to help.


    Dave

Re: Splitting multiple commands with regex failure
by bart (Canon) on Aug 24, 2012 at 08:02 UTC
    The reason why
    $command =~ m/(!\w+\s?.+?)(?=!\w)/simg;
    only returns one result is because you call it in scalar or void context, once. You either have to loop through the parts, or extract them all at once and then loop through each.

    The latter approach look easier to me. It can be done by modifying your code like:

    my @commands = $command =~ m/(!\w+\s?.+?)(?=!\w)/simg;
    Except that you'll miss the last command because there's no "!word" pattern following it. You can fix that by allowing that it's followed by either a new command, or the end of the string.
    my @commands = $command =~ m/(!\w+\s?.+?)(?=!\w|\z)/simg;
    n.b. The flags /i and /m are superfluous, and 1) there's nothing case sensitive and 2) there are no "^" or "$" meta characters that can change meaning.

    Anyway, once you have the list of commands, you can go through them like

    foreach my $command (@commands) { ... }
    and process them pretty much as you do now; reusing the $command variable name (it's a different variable, regardless) allows to keep the remainder of the code as is.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://989437]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2014-07-13 00:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (244 votes), past polls