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

Basic but stupid problem i cannot wrap my head around

by baxy77bax (Deacon)
on Aug 06, 2020 at 08:57 UTC ( [id://11120409]=perlquestion: print w/replies, xml ) Need Help??

baxy77bax has asked for the wisdom of the Perl Monks concerning the following question:

hi,

so the problem is the folloving:

perl -le '$t="a/b"; if ($t =~/^(.*?)\//){$z=$1}elsif($t=~/\/(.*)/){$z= + $1} print $z' #prints a perl -le '$t="/b"; if ($t =~/^(.*?)\//){$z=$1}elsif($t=~/\/(.*)/){$z= +$1} print $z' #prints nothing
it is probably something stupid , as i am quite tired but why isn't the second case being evaluated (or it is but why is it returning 0 )

thnx

baxy

Replies are listed 'Best First'.
Re: Basic but stupid problem i cannot wrap my head around
by Corion (Patriarch) on Aug 06, 2020 at 09:06 UTC

    In your first input string, there is an a, this gets matched and then assigned to $z (and printed).

    In your second input string, there is an empty string before the /, and no a. This gets matched and then assigned to $z (and printed).

    Maybe you don't want the stuff in front of the slash to be empty?

    perl -le '$t="/b"; if ($t =~/^(+*?)\//){$z=$1}elsif($t=~/\/(.*)/){$z=$ +1} print $z'
Re: Basic but stupid problem i cannot wrap my head around
by jeffenstein (Hermit) on Aug 06, 2020 at 16:50 UTC

    Are you expecting "b" for the second example? /^(.*?)\// matches both strings, but $1 in the second example is the zero-length string before the slash. Maybe you meant /^(.+?)\//?

Re: Basic but stupid problem i cannot wrap my head around (oneliners)
by Anonymous Monk on Aug 06, 2020 at 10:17 UTC
    Its the oneliners that stun. Shake it off. Make a file.
Re: Basic but stupid problem i cannot wrap my head around
by perlfan (Vicar) on Aug 11, 2020 at 01:33 UTC
    I find it hard to believe this is the general case of what you're attempting. But if it is, both cases will be covered if you just split on / and print the first element.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11120409]
Approved by Athanasius
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-03-29 05:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found