Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

extracting data from string

by prassi (Acolyte)
on Jul 16, 2012 at 10:48 UTC ( #982002=perlquestion: print w/replies, xml ) Need Help??
prassi has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks,

I have a string which can take of 2 forms which I have mentioned below, which is part of the code

$def1 = "!defined(FLOAT_CODE)"; $def2 = "defined(FIXED_CODE)";
The string can be either of 2 forms and I need to split in the first case like


and in the second case like


but a single statement has to do it, I do not want 2 separate case for each one. Below is my perl code with different variant I have tried.


m/^(!defined|defined)\((.*?)\)$/; print "$1----$2\n";
This gives me like
output case1: !defined----FLOAT_CODE output case2: defined----FIXED_CODE
Variant 2:
m/^(\!|defined)(.*?)$/; print "$1----$2\n";
output case1: !----defined(FLOAT_CODE) output case2: defined----(FIXED_CODE)

But both are not helping me in solving the issue can you please help in solving this or any other method avaliable.



Replies are listed 'Best First'.
Re: extracting data from string
by moritz (Cardinal) on Jul 16, 2012 at 10:59 UTC
Re: extracting data from string
by Marshall (Abbot) on Jul 16, 2012 at 13:37 UTC
    You don't give the input and the expected output.
    Do that.
Re: extracting data from string
by Neighbour (Friar) on Jul 16, 2012 at 14:33 UTC
    You mean you want to do s/[()]|defined//g; ?
    Edit: or a more refined s/defined[(]|[)]$//g;
Re: extracting data from string
by kcott (Chancellor) on Jul 17, 2012 at 06:23 UTC

    You might find regexes easier to read with the x modifier (see perlre).

    $ perl -Mstrict -Mwarnings -E ' for (qw{!defined(FLOAT_CODE) defined(FIXED_CODE)}) { m{ \A ( [!]? ) defined [(] ( .*? ) [)] \z }x; say $1 ? qq{$1 $2} : $2; } ' ! FLOAT_CODE FIXED_CODE

    -- Ken

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://982002]
Approved by moritz
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-10-20 04:31 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (258 votes). Check out past polls.