Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: help with a regex

by toolic (Bishop)
on Feb 01, 2013 at 18:16 UTC ( #1016596=note: print w/replies, xml ) Need Help??

in reply to help with a regex

You are printing the return value of s///, which is the number of substitutions made. If there was only 1 substitution, it prints 1, which just happens to match the hash value. You should print $_ instead:
use warnings; use strict; my %words = (word => 1, word2 => 1, word3 =>1); my $temp = 'WORD2'; my $temp1 = 'word2'; $_ = 'here is word2'; s/$temp1/$words{$temp1} ? "$temp " : "[$temp] "/ex; print "$_\n"; __END__ here is WORD2

Replies are listed 'Best First'.
Re^2: help with a regex
by Athanasius (Chancellor) on Feb 02, 2013 at 03:27 UTC
    You should print $_ instead

    Or, just add the /r modifier to the substitution:

    #! perl use v5.14; use warnings; use strict; my %words = (word => 1, word2 => 1, word3 => 1); my ($temp, $temp1) = qw(WORD2 word2); $_ = 'here is word2'; say s/$temp1/$words{$temp1} ? "$temp " : "[$temp] "/er;


    13:14 >perl here is WORD2 13:15 >

    From s/PATTERN/REPLACEMENT/msixpodualgcer in Regexp Quote Like Operators:

    r Return substitution and leave the original string untouched.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      It should be made clear that the  s///r regex switch is only available with Perl versions 5.14 and above.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1016596]
[markong]: " Perl: the Markov chain saw" :D aha
[Discipulus]: thanks 1nickt but seems i'm not able to use it correctly...
[Discipulus]: Eily i'm testing a failure: i ok if i get bad results.. perhaps there is another way to do it...
[Discipulus]: in other words: i have a contructor that carps on missing arguments and I want to test that it fails correctly, without carping out in the test output

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2018-01-22 11:28 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (233 votes). Check out past polls.