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

I cant get the following code to cooperate:

#!/bin/perl use v5.12; use warnings;

MY CODE:

my $Namelist = 'Wilma Fred Barney Betty Dino'; if ( $Namelist ~= m/\w+a/) { print "Matched|$<$&>$|\n"; say "The name that matched was $1 \n"; } else { print "No match: \n"; }

My error(s)

syntax error at ./exer_8_3 line 8, near "$Namelist ~" syntax error at ./exer_8_3 line 11, near "}"

TIA,
Catfish

2018-10-20 Athanasius added code and paragraph tags

Replies are listed 'Best First'.
Re: Simple prog isnt cooperating
by pryrt (Abbot) on Oct 15, 2018 at 21:50 UTC

    I see three typos/issues:

    1. $Namelist ~= m/\w+a/ should be $Namelist =~ m/\w+a/ -- tilde after equal sign for pattern match
    2. print "Matched|$<$&>$|\n" should be print "Matched|$<$&$>|\n" -- sigil before greater-than, not after, for special variable
    3. You have no parenthetical phrases in your match, so $1 doesn't match anything. If you want to get the name, use parens around the match: m/(\w+a)/

    (stevieb beat me to #1 and #3)

      Wait, I don't think you want most of those special variables in your print. ($< being UID, $> being EUID, $| being autoflush... I think you did mean $& as the match). Were you trying to escape the <, >, and/or | ? What were you trying to do with those?

        Sir,

        What I'm trying to accomplish is to pull a name from the list and print it out. I have done this various ways, but I'm trying to do this as requested in chapter 8, exercise #3 in the 'Learning Pearl' book by O'Reilly. I have corrected all of the typos that were pointed out,(very humbling that I did not pick up on that). I am still running into problems.

        #!/bin/perl use v5.12; use warnings; my $Namelist = 'Wilma Fred Barney Betty Dino'; if ( $Namelist =~ m/(\w+a)/ { print "Matched|$<$&$>|\n"; say "The name that matched was $1 \n"; } else { print "No match: \n"; } syntax error at ./exer_8_3 line 8, near "m/(\w+a)/ {" syntax error at ./exer_8_3 line 11, near "}"
        Thanks again, Catfish

      You (pryrt) are one of the most loyal, honest and trustworthy persons I've ever met regarding code. I'm sorry I beat you to the punch here ;)

      You have not only "assisted" me on extremely major projects, but actually wrote, and re-wrote code on behalf of our (yes, our) major berrybrew project, including *massive, major and new* test scenarios and functionality.

      If I could give you the Perlmonks XP, I would :P

        Thanks. :-) (And being beat to the punch doesn't really bother me.)

Re: Simple prog isnt cooperating
by stevieb (Canon) on Oct 15, 2018 at 21:45 UTC

    In if($Namelist ~= ...), I think you meant:

    =~. That's the "equals or contains" regex check. ~= doesn't mean much to perl.

    Also, $1 is a special variable based on a regex capture. You're not capturing anything here. A capture in a regex is specified by parens. I've got a meeting to bounce to, so someone else will have to explain that issue.

      Yet Another Boring Meeting (where when I'm not needed, I hack Perl). Here's the capture you need. Note that it was quick and dirty, so I have not inserted any checking at all (which should be promoted; always check the value of a capture):

      use v5.12; use warnings; my $Namelist = 'Wilma Fred Barney Betty Dino'; if ( $Namelist =~ m/(\w+a)/) { print "Matched|$<$&>$|\n"; say "The name that matched was $1 \n"; } else { print "No match: \n"; }

      Output:

      spek@scelia ~/scratch $ perl format.pl Matched|1000Wilma>0 The name that matched was Wilma
Re: Simple prog isnt cooperating
by stevieb (Canon) on Oct 15, 2018 at 21:38 UTC

    Welcome to the Monastery, catfish1116!

    Please put your programming code within <code>code here...</code> code tags, per Writeup Formatting Tips.

    For the Monks, here it is formatted and within proper tags:

    use v5.12; use warnings; my $Namelist = 'Wilma Fred Barney Betty Dino'; if ( $Namelist ~= m/\w+a/) { print "Matched|$<$&>$|\n"; say "The name that matched was $1 \n"; } else { print "No match: \n"; }
      Thank you for your help.

      I'm not proficient in HTML, but promise to do better in future posts

      Catfish