CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
=~ s ????



Feb 14, 2001, 4:13 PM

Post #1 of 2 (544 views)
=~ s ???? Can't Post

Hi all :-)

I have a question about searching in Perl

For example :

Lets say a have a text file like

What I want to do now is to find out how many lines that contains ip number
Itīs 3 okej, but how do I get Perl to understands it ?

User / Moderator

Feb 14, 2001, 6:38 PM

Post #2 of 2 (539 views)
Re: =~ s ???? [In reply to] Can't Post

Consider this block of code...


use strict;

my $source = <<EOF;

my @rows = split ("\n", $source);
my $ip = '';

my $count;

Now here are three options that might do what you want to @rows...

foreach (@rows) { 
$count++ if (/$ip/);

print "'$ip' found on $count of ", scalar (@rows), " lines\n";

$count = ($source =~ s/$ip/$ip/g);

print "'$ip' found on $count of ", scalar (@rows), " lines\n";

$count = grep { /$ip/ } @rows;

print "'$ip' found on $count of ", scalar (@rows), " lines\n";

I made a small change to the data so one of these three solutions will give the wrong answer. But learning which one is left as an exercise for the reader.

Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives