Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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++; }

Comment on regex not working
Select or Download Code
Re: regex not working
by AnomalousMonk (Abbot) 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!)

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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2015-04-26 19:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who makes your decisions?







    Results (488 votes), past polls