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

I the Learning Perl book it gives the following question: Make a pattern that will match three consecutive copies of whatever is curently contained in $what. That is, if $what is fred, your pattern should match fredfredfred or barneyfredfred or barneybarneybarney or any other variations. Hint: you should set $what at the top of the pattern test program with a statment like:
my $what = 'fred|barney';
I have tried this problem with the solution that they gave in the book but it didn't work does anyone know how this script my look.. Warren E Bullock III wbullock@twcny.rr.com

Replies are listed 'Best First'.
•Re: A pattern that matches three consecutive copies...
by merlyn (Sage) on Jun 09, 2002 at 15:43 UTC
    I have tried this problem with the solution that they gave in the book but it didn't work
    Since the answer given there has been presented in classrooms hundreds of times to thousands of people and read by probably a hundred times that many again home readers who bought the book and worked through the exercises, and you would be the first to say it "didn't work", I suspect something is more off kilter in your transcription or understanding of the answer than something wrong in the book. Just looking at the odds. {grin}

    Please post precisely what you tried, what you expected, and what you got. I bet we can sort it out from there.

    -- Randal L. Schwartz, Perl hacker

      In the book under appendix A it says: Here is one way to do it:
      /($what){3}/
      so this is what I tried to do in my script:
      #!/usr/bin/perl -w use strict; my $what = 'fred|barney'; if($what =~ /($what){3}/){ print "$&"; } else{ print "no match\n"; }
        Well, fred|barney as a string doesn't have three things that match the regex (fred|barney) in a row. So I would expect it to say "no match". Are you getting something different from that?

        -- Randal L. Schwartz, Perl hacker