----- Original Message -----
From: "John W. Krahn" <[EMAIL PROTECTED]>
> Leon wrote:
> >
> > ----- Original Message -----
> > From: "Mark Mclogan" <[EMAIL PROTECTED]>
> > > How I can know in that I number of line finds a word in a text file?.
> > > For example, a file "file.txt" contains the following list:
> > > Chocolate
> > > Cake
> > > Cheese
> > > Apple
> > > How I can know in that line number is the Apple word?
> >
> > open FILE, 'file.txt' or die "$!\n";
> > while (<FILE>){
> > chomp;
> > my $count++;
>
> $count is lexically scoped to the while loop so it will always have a
> value of one.
Thanks John for pointing out my mistakes otherwise I would still be in the
dark. I was wrong when I thought that the variable $count within the while
loop is avaliable to all parts of this while loop such as this :-
while (<FILE>){
chomp;
my $count++;
if ( 1 ) {print "Line number $count\n"} ; #I was wrong!
};
I apologise to members for my unintentional mistake.
Using strict; apart from defining "my variable" in the main body of the
script, could some member tell me :-
(1) how to make a variable within loops non-lexical as in this eg:-
while (<FILE>){
my $count++; # how to globalised this variable?
};
(2) how to make a variable available to all parts of this while loop
ONLY.... eg:-
use strict;
while (<FILE>){
my $count++;
if (1){
#what must I do to return $count here.
$count_available_here_also = $count;
};
};
#I do not wish to return $count here..
$but_not_here = $count;
Thanks
--- The correction of my untested failed script ----
--- Tested and working ----
open FILE, 'file.txt' or die "$!\n";
my ($count);
while (<FILE>){
chomp;
$count++;
print "Apple is found in line number $count\n" if (/Apple/);
};
close FILE;
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]