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

Script
#!/usr/bin/perl -w use strict; while (my $input = <DATA>){ my ($name,$num,$addr,$date,$n) = split(/:/, $input); print "$name\n", if $name = s/William Kopf/Siegfield/ig; } __DATA__ Tommy Savage: 408–72 4–0140: 12 2 2 Oxbow Court, Sunnyvale, CA 94087: + 5/19/66: 34200 50 Lesle Kerstin: 408–456–1234: 4 Harvard Square, Boston, MA 02133: 4 +/22/62: 52600 51 Jon DeLoach: 408–253–3 122: 12 3 Park St. , San Jose, CA 94086: 7/ +25/53: 85100 52 Ephram Hardy: 293–259–5395: 2 3 5 Carlton Lane, Joliet, IL 73858: +8/12/20: 56700 53 Betty Boop: 245–836–83 57: 63 5 Cutesy Lane, Hollywood, CA 91464: +6/23/23: 14500 54 William Kopf: 846–83 6–2837: 693 7 Ware Road, Milton, PA 93756: 9/ +21/46: 43500 55 Norma Corder: 397–857–2735: 74 Pine Street, Dearborn, MI 23874: 3/ +28/45: 245700 56 James Ikeda: 834–938–8376: 2 3 445 Aster Ave. , Allentown, NJ 8374 +5: 12/1/38: 45000 57 Lori Gortz: 327–832–5728: 3465 Mirlo Street, Peabody, MA 34756: 10 +/2/65: 35200 58 Barbara Kerz: 385–57 3 –8326: 83 2 Ponce Drive, Gary, IN 83756: 12 +/15/46: 268500
Result
Use of uninitialized value $_ in substitution (s///) at ./exer1.pl lin +e 45, <DATA> line 1. Use of uninitialized value $_ in substitution (s///) at ./exer1.pl lin +e 45, <DATA> line 2. ./exer1.pl line 45, <DATA> line 8.

Can anyone explain why I am getting it. Thanks!!

Replies are listed 'Best First'.
Re: regex problem
by CountZero (Bishop) on Nov 03, 2010 at 22:51 UTC
    You probably meant
    $name =~ s/William Kopf/Siegfield/i;
    rather than
    $name = s/William Kopf/Siegfield/ig;

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: regex problem
by Utilitarian (Vicar) on Nov 03, 2010 at 22:41 UTC
    You are assigning the value of $name to the result of a substitution on the default variable $_ reread perlre and check the syntax used to perform regex operations on a variable again.

    PS: The g modifier is unnecessary in your subsitution

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: regex problem
by rnaeye (Friar) on Nov 04, 2010 at 01:05 UTC
    Try this.
    #!/usr/bin/perl use strict; use warnings; use 5.010; while (<DATA>){ my ($name,$num,$addr,$date,$n) = split(/:/, $_); $name ~~ s/William Kopf/Siegfield/ig; print "$_"; }
Re: regex problem
by locked_user sundialsvc4 (Abbot) on Nov 04, 2010 at 12:52 UTC

    Data::Dumper is sometimes your very best friend.

    use Data::Dumper; use Carp; unless(defined($name)){ print STDERR Data::Dumper->Dump( [$input, $name,$num,$addr,$date,$n], ["input", "name", "num", "addr", "date", "n"]); croak"Now let's stop and have a look at this..."; }

    The first time the program encounters what you have determined to be the error-condition, it will now print out the line of data and the variables extracted from it, and then conveniently fall-dead.

Re: regex problem
by aquarium (Curate) on Nov 03, 2010 at 23:23 UTC
    use round brackets around your if statement...without the brackets it's probably testing (if $name), and there's nothing to bind the substitution to...and perl tries to use default $_ for the substitution which is empty.
    the hardest line to type correctly is: stty erase ^H
      use round brackets around your if statement.
      No. Not needed.
      without the brackets it's probably testing (if $name),
      That doesn't make any sense.
      and there's nothing to bind the substitution to...
      And neither does that.

      Please, when answering a question, don't make wild guesses that can easily be verified (run it through -c -MO=Deparse, and you'll see you're not making any sense). Giving bad answers actually decreases the value of Perlmonks. Running the code through -c -MO=Deparse would have taken less time than typing in the answer - and hence would have saved you time. Would have saved me time as well.

      A reply falls below the community's threshold of quality. You may see it by logging in.