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 (Parson) 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.