in reply to Re: Simple prog isnt cooperating
in thread Simple prog isnt cooperating

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?

Replies are listed 'Best First'.
Re^3: Simple prog isnt cooperating
by catfish1116 (Beadle) on Oct 16, 2018 at 15:23 UTC
    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.

        I'm not sure what you're using to edit, but a good text editor with color-coding and highlighting can be really useful in avoiding this stuff and tracking it down when it happens.

        I use Notepad++ and Vim (when I have to edit via an SSH terminal). I've heard good things about Visual Studio Code too