Re: Any wrong?

From: Adam (lists_acc_at_comcast.net)
Date: 05/30/04


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



Relevant Pages

  • Re: A couple of expect novice questions
    ... Thanks a lot for responding. ... And thanks for posting the script. ... ever hear that old saying about "giving a man a fish" versus "teaching ... can't be understood by even an experienced programmer _without_ said ...
    (comp.lang.tcl)
  • Re: There were days of slavery, perhaps I am free (payments not counting)
    ... the first plane back to Australia. ... get on a bus, and put on loud hispanic music. ... live fish into your pants, where it wiggles, and you dance. ... But I came to talk about a script. ...
    (sci.astro)
  • Re: system load spike
    ... Depending on the script you could introduce a "sleep 1" between each ... Therefore it's difficult to insert "sleep" into there. ... Give a man a fish, and he eats for a day. ... To UNSUBSCRIBE, email to debian-user-REQUEST@xxxxxxxxxxxxxxxx ...
    (Debian-User)
  • Re: Help with manipulating php output
    ... > created by the PHP processor. ... > I have a script we shall call a.php. ... SO - if you can't get around the encryption, ... Catch a man a fish, you feed him for a day. ...
    (alt.php)