Re: Any wrong?
From: Randy W. Sims (ml-perl_at_thepierianspring.org)
Date: 05/30/04
- Next message: Rob Richardson: "Why didn't 'use strict' catch this error?"
- Previous message: Jame brooke: "Any wrong?"
- In reply to: Jame brooke: "Any wrong?"
- Next in thread: Adam: "Re: Any wrong?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 30 May 2004 14:30:12 -0400 To: Jame Brooke <muarlove@yahoo.com.hk>
Jame Brooke wrote:
> Friend, anybody have idea regarding this problem. Assume I want know where the word call "fish" locate in which line number, and this word i save under file call nlexample. Assume we already know fish save under line number 2, how we show this?
>
> Example:nlexample
>
> I love vegetable
> I love fish
> I love perl
>
>
> my script
> -----------
> my $lineno;
>
> while (<>){
> if (/pattern/){
> print $lineno++;
> print ": $_";
> }
> }
>
> As i know we can use pattern tester to matching right? But why i perl no allow me compiler?
>
> [root@localhost root]# ./pattern nlexample fish
> Can't open fish: No such file or directory at ./pattern line 7, <> line 6.
>
>
> Any comment regarding this problem, please advise.
If "fish" is the string you're looking for, you need to grab it off the
command line. Otherwise, the <> operator will think it is a file and try
to open it. Given your example usage above and my understanding of what
you're trying to do, the code below should work.
Randy.
#!/usr/bin/perl
use strict;
use warnings;
my $pattern = pop( @ARGV );
while (defined( my $line = <ARGV> )) {
chomp( $line );
if ( $line =~ /$pattern/o ) {
printf( "%4d: %s\n", $., $line );
}
} continue {
close( ARGV ) if eof;
}
__EOF__
- Next message: Rob Richardson: "Why didn't 'use strict' catch this error?"
- Previous message: Jame brooke: "Any wrong?"
- In reply to: Jame brooke: "Any wrong?"
- Next in thread: Adam: "Re: Any wrong?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|