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

Hi perl monks, I was searching the archives to find a solution to substitute strings that don't contain other specific strings and i came across stuff like

unless ($string =~$flag) s/$string/"yay found it"/g;

However, that didn't seem to work. Anyone know why? What i specifically want is to append the year '2003' to the end of a list, if it is not already included in the string. However, the tricky thing is I can't just search for the string 2003, because it might be tacked on as a range eg. 2000-2003. Any solutions are welcome.

Thanks!

jc

Replies are listed 'Best First'.
Re: strings that dont contain
by blue_cowdawg (Monsignor) on Jul 24, 2003 at 18:53 UTC

    Is this what you mean?

            #!/usr/bin/perl -w ######################### use strict; while (my $line=<DATA>) { chomp $line; unless ( $line =~ /2003/ ) { $line =~ s/($line)/$1 2003/; } print $line,"\n"; } __END__ Copyright 2001 Copyright 2001-2003 Copyright 2001,2002,2003 Copyright 1999-2001

    When run yields:

          --$ perl tack_on.pl Copyright 2001 2003 Copyright 2001-2003 Copyright 2001,2002,2003 Copyright 1999-2001 2003


    Peter L. BergholdBrewer of Belgian Ales
    Peter@Berghold.Netwww.berghold.net
    Unix Professional
      yep! That's exactly what I'm trying to do. But I had a little more complicated substitution regex that included the appending date part. What i did was just take out the unless statement from the substitution one ( i put it all together) and just checked unless ($1=~ /2003/) and it worked perfectly.

      thanks for the tip!

      jc

Re: strings that dont contain
by Limbic~Region (Chancellor) on Jul 24, 2003 at 19:15 UTC
    jc23,
    If you are trying to determine if a string contains a substring - then you probably want to use substr index instead of a regex.
    #!/usr/bin/perl -w use strict; my $string = 'copyright 2001, 2002'; if ( index($string,'2003') == -1 ) { $string .= ', 2003'; }
    The part I don't understand (and apparently neither did Dragonchild) is why you can't just search for 2003. Are you saying that if it is part of a range 2000-2003 that you want to add it again?
    #!/usr/bin/perl -w use strict; my $string = 'copyright 2000-2003'; $string .= ', 2003' if $string ~= /\b2003\b/;
    If you were a little more clear on exactly what you wanted to do - with examples of different cases, we would be glad to be of more help.

    Cheers - L~R

    update: Thanks to tedrek for pointing out substr should have been index. /me places dunce hat on and sits in corner

Re: strings that dont contain
by roju (Friar) on Jul 24, 2003 at 20:03 UTC
    However, the tricky thing is I can't just search for the string 2003, because it might be tacked on as a range eg. 2000-2003.

    So for a range 2000-2004, do you still want to tack on the 2003? You'll have to start parsing the lines if you do.

Re: strings that dont contain
by LameNerd (Hermit) on Jul 24, 2003 at 19:04 UTC
    Don't forget about !~
    bash-2.03$ ./test.pl one bone one does NOT end in bone bash-2.03$ ./test.pl bone one bone does end in one bash-2.03$ ./test.pl bonetwo one bonetwo does NOT end in one bash-2.03$ ./test.pl bonetwo two bonetwo does end in two bash-2.03$ cat test.pl #!/usr/local/bin/perl -w use strict; my $s1 = shift; my $s2 = shift; if ( $s1 !~ /$s2$/ ) { print "$s1 does NOT end in $s2\n"; } else { print "$s1 does end in $s2\n"; }
Define your problem first! (was Re: strings that dont contain)
by dragonchild (Archbishop) on Jul 24, 2003 at 18:52 UTC
    You're going to have to define your problem better. How do you know when you are visually inspecting the string if 2003 should be added? Once you can lay out a series of steps you would tell your friend to do if he was doing this by hand, then you can tell your friend the computer what to do. :-)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.