Re: Any wrong?
From: Adam (lists_acc_at_comcast.net)
Date: 05/30/04
- Next message: Andrew Gaffney: "Re: [Socket Programming]: Need info for Client / Server scenario"
- Previous message: Rob Richardson: "Why didn't 'use strict' catch this error?"
- In reply to: Jame brooke: "Any wrong?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 30 May 2004 17:20:01 -0400 To: Jame Brooke <muarlove@yahoo.com.hk>
Jame
> 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?
Ugh, you're killing me here with your grammar, but I think I understand
what you're asking.
> my $lineno;
>
> while (<>){
> if (/pattern/){
> print $lineno++;
> print ": $_";
> }
> }
First about your script, either camel case your variables or use
underscores. Next, you are post incrementing your variable, which will
print the current value of the variable THEN increment it by 1. So
you're program would show a line number of 1 instead of 2 for your
file. What you really want is to pre-increment the variable
(++$var_name). Finally, that print statement could really be one line.
As a side note, its a good thing you're not using`chomp()`on the values
in the file or your lack of a carriage return at the end of the `print
":$_";` line would cause all your output to end-up on the same line.
So re-write your script to this:
#! /usr/bin/perl
use strict;
my $line_no = 0;
while (<>) {
chomp;
print (++$line_no . ": $_\n") if (/pattern/);
}
One more thing, if you don't assign a value of 0 to $line_no you will
get a warning saying something about the variable never being used.
Just to be safe, I'd assign it a value of `0` even though the default
is undef.
> 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.
You're getting this error because there isn't a file named `nlexample`
or `fish` in the current directory where the program is.
Regards,
Adam
- Next message: Andrew Gaffney: "Re: [Socket Programming]: Need info for Client / Server scenario"
- Previous message: Rob Richardson: "Why didn't 'use strict' catch this error?"
- In reply to: Jame brooke: "Any wrong?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|