Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Regex random values pattern match and code efficiency

by Mark.Allan (Sexton)
on Sep 12, 2013 at 13:47 UTC ( #1053726=perlquestion: print w/replies, xml ) Need Help??
Mark.Allan has asked for the wisdom of the Perl Monks concerning the following question:

I know this is usualli a big NO NO but Im reposting a post I posted last night because I missed critical information out

The link to the orginal post is here ----------->

http://www.perlmonks.com/?node_id=1053560

The part I missed out was related to question 1. I need only the values between {} for DEPLOY_PARAMS and not the entire string, this obvuously put a different complexion on things because values are not static

Any assistance would be much appreciated

  • Comment on Regex random values pattern match and code efficiency

Replies are listed 'Best First'.
Re: Regex random values pattern match and code efficiency
by hdb (Monsignor) on Sep 12, 2013 at 14:00 UTC

    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

      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
Node Status?
node history
Node Type: perlquestion [id://1053726]
Approved by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2017-12-14 06:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (384 votes). Check out past polls.

    Notices?