Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

! FLOAT_CODE

and in the second case like

FIXED_CODE

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.

Variant:1

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.

Regards,

-Prassi

Comment on extracting data from string
Select or Download Code
Re: extracting data from string
by moritz (Cardinal) on Jul 16, 2012 at 10:59 UTC
Re: extracting data from string
by Marshall (Prior) 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 (Abbot) 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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-12-28 23:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (183 votes), past polls