×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Strings

Strings

Strings

(OP)
Hi
I have a little problem with a string.
I have a text file with names like this:


benny
fredrik
bodil
harry
bill
susan
jenny

I now how to use $lines =~ s /bill/billy_boy/g;
bill will be billy_boy

But how do I do when a want to replace all names expect bill to
become billy_boy

When finished it should look like this

billy_boy
billy_boy
billy_boy
billy_boy
bill
billy_boy
billy_boy



/Thanks

RE: Strings

i'm sure there's an easier way to do this, but...


@file = <FILE>;

foreach(@file)
{
    if($_ =~ /bill/g)
    {

    }
    else
    {
       s/.+/billy_boy/g;
    }
}

adam@aauser.com

RE: Strings

One step easier yes....
i'm sure there's an easier way to do this, but...

This will work fine, the !~ or not match .. is what you are looking for...

Jams

@file = <FILE>;
$i = 0;
while (@file$i)
{
  if(@file[$i] !~ /bill/gi)
  {
    @file[$i] = "billy_boy";
  }
}
Falazar@yahoo.com

RE: Strings

You can always solve one problem lots of different ways in Perl - here's another solution - slight variation on the if:

my @a = ("abc", "bill", "xyz");
my @b = @a;
foreach (@b) {
   s/.+/billy_boy/ if ($_ !~ /bill/);
}
print "Before: array = " . join(", ", @a) . "\n";
print "After: array = "  . join(", ", @b) . "\n";                               

Here's the output:
-------------
Before: array = abc, bill, xyz
After: array = billy_boy, bill, billy_boy            

I actually prefer not to use this method of specifying the "if" after the "then" part, but it is available and it does work, so I thought I'd present it.  It does make for shorter code.               


Hardy Merrill
Mission Critical Linux, Inc.
http://www.missioncriticallinux.com

RE: Strings

I see what you want, you want to say:

s/^(not bill)$/billy_boy/

 I'm pretty sure this is what negative lookaheads are for:

@arr = qw(abc bill xyz);
foreach (@arr) {
s/^(?!bill).*$/billy_boy/;
}
print @arr;

 This works perfect in my script.  (?!...) means that it will match the current position only if the next thing the regex engine finds does not match '...'
 Hope this is what you want.


"If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close