Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

regex not working

by andrewbriggs (Initiate)
on Oct 07, 2011 at 04:36 UTC ( #930105=perlquestion: print w/replies, xml ) Need Help??
andrewbriggs has asked for the wisdom of the Perl Monks concerning the following question:

I have tried for hours to solve this. Ok, the user enters a number, say 2020. 2020 is put into an array. {2, 0, 2, 0}. At @array[0] = 2; 0 occurs twice in the string so this should print true; @array1 1 occurs 0 times in the string so this should return true too. and @array[2] 2 occurs 2 times in the string and this returns true; But when I enter 2020 , I get false true false true. I've looked at this for hours.
#!/usr/bin/perl -w print "Enter number : "; my $num = <STDIN>; chomp($num); @nums = split /(?<=[\d])/, $num; $i = 0; foreach (@nums) { if ($num =~ /[$i]{$_}/) { print "true" } else { print "false"; } $i++; }

Replies are listed 'Best First'.
Re: regex not working
by jwkrahn (Monsignor) on Oct 07, 2011 at 07:54 UTC

    Well let's see what your program does:

    $ perl -le' use warnings; use strict; print "Enter number : "; chomp( my $num = <STDIN> ); print "\$num = $num"; my @nums = split /(?<=[\d])/, $num; print "\@nums = @nums"; my $i = 0; for ( @nums ) { my $regex = qr/[$i]{$_}/; print "\$regex = $regex"; print $num =~ $regex ? "true" : "false"; $i++; } ' Enter number : 2020 $num = 2020 @nums = 2 0 2 0 $regex = (?-xism:[0]{2}) false $regex = (?-xism:[1]{0}) true $regex = (?-xism:[2]{2}) false $regex = (?-xism:[3]{0}) true

    On the first pass you are trying to match '2020' with /[0]{2}/, or in other words with /00/.    Since '2020' does not contain the string '00' the program prints false.

    On the second pass you are trying to match '2020' with /[1]{0}/, or in other words with //.    Since '2020' contains five places that will match the program prints true.

    $ perl -le'$x = () = "2020" =~ /[1]{0}/g; print $x' 5

    The next two follow the same pattern as the first two.

Re: regex not working
by AnomalousMonk (Chancellor) on Oct 07, 2011 at 05:14 UTC

    Let's look at the  /[$i]{$_}/ regex. The  [$i] defines a character class that matches whatever  $i interpolates as a string into the regex. In this case,  $i never changes: it is always the number 0 which interpolates as the '0' (zero) character, so the character class matches a '0' character and nothing else.

    The  {$_} quantifier of the  [0] character class (which is the same as '0') takes on the values that  $_ interpolates into the regex on each iteration of the for-loop. These values are from the  @nums array and happen to be  (2, 0, 2, 0) successively.

    So the  [$i]{$_} regex expression alternates between  0{2} and  0{0}. That's all there is to the regex. The  0{2} regex does not match (twice!) against the  $num string of '2020' because that string does not have two consecutive '0' characters anywhere in it. The  0{0} regex does match (again, twice) because there is a place in the '2020' string where there is no '0' character.

    See perlre, perlretut, perlrequick.

    Update: Rats. Missed the  $i++; statement. Of course, the analysis of jwkrahn is correct, while mine is wandering up and down in the street outside the ballpark. .oO(But was  $i++; present in the original original post, the one to which I replied? The more I think about it, the more I think it wasn't!)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://930105]
Approved by baxy77bax
Jar. Jar!...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2018-04-26 21:57 GMT
Find Nodes?
    Voting Booth?