Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

extract options from a simple string

by merlyn (Sage)
on Jul 01, 2002 at 12:37 UTC ( [id://178529]=CUFP: print w/replies, xml ) Need Help??

Someone on alt.perl asked:
What is elegant way of convert string like "--param1 blabla --param2 bla bla blo" Into assoc array $a{"param1"} == blabla and $a{"param2"}== "bla bla blo" ? Param values never starts with "--".
Here's my reply.
my $string = "--param1 blabla --param2 bla bla blo"; my %newhash = $string =~ /--(\w+)\s+(.*?(?=\s*$|\s*--))/g; for (sort keys %newhash) { print "$_ = <$newhash{$_}>\n"; }

Replies are listed 'Best First'.
Re: &bull;extract options from a simple string
by svad (Pilgrim) on Jul 01, 2002 at 14:28 UTC
    Here is another one, which may be of interest in a light of that task: alternate parsing method for options
    I start 80% of my small utilities programs with that lines and found them convenient enough.

    Best wishes,
    svad

      Ahh, but that one's looking at an already split @ARGV, for which there are dozens of solutions (even some I've hand-rolled). The poster wanted something that comes from a single string.

      -- Randal L. Schwartz, Perl hacker

        I definitively need glasses if I'm going to read that code snippet...or a bigger monitor...

        #!s #!s, oh baby when she moves, she moves...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://178529]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-25 05:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found