in reply to Simple prog isnt cooperating

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)

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

    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

        Catfish, you still have a typo: you dropped the closing parenthesis for the if() block.

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

        This code results in:

        Matched|0Wilma0| The name that matched was Wilma

        Note the strange "0" before and after "Wilma". I am in Windows, so I don't get a $< UID or a $> EUID. As I pointed out, those varibles make no sense in the context of trying to print matched output. When I asked what you "what were you trying to do with those", I was trying to get clarification of what you wanted your output to look like. Were you trying to have angle brackets literally used in your output? like:

        Matched|<Wilma>| The name that matched was Wilma
        That would be accomplished with print "Matched|<$&>|\n";

        Or were you trying to have literal dollar signs and literal angle brackets? like:

        Matched|$<Wilma>$| The name that matched was Wilma
        That would be accomplished with print "Matched|\$<$&\$>|\n";

        Or did you just really want Wilma to be printed between the vertical bars? like:

        Matched|Wilma| The name that matched was Wilma
        That would be accomplished with print "Matched|$&|\n";

        Or did you really want the linux user-id number ($<) and effective user-id number ($>) to surround Wilma? like:

        Matched|###Wilma###| The name that matched was Wilma
        (where ### are actual numbers, not the number-signs I have shown): That would be accomplished with print "Matched|$<$&$>|\n";

               if ( $Namelist =~  m/(\w+a)/  {

        You have opened 2 sets of brackets but only closed one.

        if ( $Namelist =~ m/(\w+a)/ ) {
        Thanks for all PerlMonks that posted, (yet another stupid typo). I am forever in your debt.
Re^2: Simple prog isnt cooperating
by stevieb (Canon) on Oct 15, 2018 at 22:06 UTC

    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.)