Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Regex random values pattern match and code efficiency

by hdb (Monsignor)
on Sep 12, 2013 at 14:00 UTC ( [id://1053731]=note: print w/replies, xml ) Need Help??


in reply to Regex random values pattern match and code efficiency

Trying to understand your question: In the first post you were told how to find the right hand side of DEPLOY_PARAMS=$DOMAIN{HOST},$DOMAIN{IP},$DOMAIN{CONFIG} and now you want to extract what's within the braces? Like this:

use strict; use warnings; my $str='$DOMAIN{HOST},$DOMAIN{IP},$DOMAIN{CONFIG}'; my @result = $str =~ /{(.+?)}/g; print "@result\n";

Update: Putting it all together:

use strict; use warnings; use Data::Dumper; my %h = map {my($k, $v)=/^(.*?)=(.*)/; $v=[ $v=~/{(.+?)}/g ] if $v=~/} +/; $k=>$v } <DATA>; print Dumper \%h; __DATA__ DEPLOY_TIME=300 DEPLOY_TYPE=OS DEPLOY_PARAMS=$DOMAIN{HOST},$DOMAIN{IP},$DOMAIN{CONFIG} DEPLOY_DEFAULT_STARTUP=$DOMAIN{STARTUP.INI} DEPLOY_DEBUG=Y DEFAULT_EXPIRY=900

Replies are listed 'Best First'.
Re^2: Regex random values pattern match and code efficiency
by Mark.Allan (Sexton) on Sep 12, 2013 at 18:04 UTC

    Yes, extract values which are in braces only BUT the same values will not be there every time, for example, another file will only contain the following

    DEPLOY_PARAMS=$DOMAIN{HOST},$DOMAIN{IP}

    Or

    DEPLOY_PARAMS=$DOMAIN{HOST}

    For example, the values could change in every file so its not static mapping

      Well, I encourage you to run my proposed code on all kinds of test data to see how it fares. You might also try to understand how it works to see that it does not care what exactly there is. As you will see there is no sign of a static mapping.

      One thing I would propose is to add a filter for lines without equal sign to make it more robust:

      my %h = map {my($k, $v)=/^(.*?)=(.*)/; $v=[ $v=~/{(.+?)}/g ] if $v=~/} +/; $k=>$v } grep {/=/} <DATA>;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-18 05:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found