RE: Question!! number of line....
--- "WANG, SHIPING [AG/1000]" <[EMAIL PROTECTED]> wrote: > Hi, Question for your code: > > what is the meaning "require 5;" Require with just a version number means that you must be running Perl 5 or better. For example, let's say I was to use the 'our' keyword. That was introduced in 5.6, so I might put this at the top of my code: require 5.6; our $global; Cheers, Curtis "Ovid" Poe = "Ovid" on http://www.perlmonks.org/ Someone asked me how to count to 10 in Perl: push@A,$_ for reverse q.e...q.n.;for(@A){$_=unpack(q|c|,$_);@a=split//; shift@a;shift@a if $a[$[]eq$[;$_=join q||,@a};print $_,$/for reverse @A __ Do You Yahoo!? Send FREE video emails in Yahoo! Mail! http://promo.yahoo.com/videomail/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Question!! number of line....
Hi, Question for your code: what is the meaning "require 5;" -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] Sent: Friday, December 28, 2001 3:54 PM To: [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Subject: RE: Question!! number of line require 5; sub get_line_num { my ($file_name, $string_to_look_for) = @_; my $line_num = -1; open ( MYFILE, $file_name ) or die "Error opening file '$file_name'. Reason is <$!>"; while ( ) { if ( /$string_to_look_for/i ) { $line_num = $.; last; } } close( MYFILE ); return $line_num; } my $num = get_line_num( 'file.txt', 'apple' ); print "The word 'apple' was found in line number '$num'\n"; - [Sathish] -Original Message- From: Mark Mclogan [mailto:[EMAIL PROTECTED]] Sent: Friday, December 28, 2001 1:37 PM To: [EMAIL PROTECTED] Subject: Question!! number of line 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 orange melon lemon How I can know in that line number is the Apple word? Over MMClogan _ MSN Photos is the easiest way to share and print your photos: http://photos.msn.com/support/worldwide.aspx -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: use strict, How to 'globalised' variable within loops. (was => Re: Question!! number of line....)
"Leon" <[EMAIL PROTECTED]> writes: [...] > (1)how to make a variable within loops non-lexical as in this eg:- > while (){ > my $count++; # how to globalised this variable? $count++; # NO "my", therefore implicitly global. > }; > > (2)how to make a variable available to all parts of this while loop > ONLY eg:- > use strict; > while (){ ^ $count scope starts here > my $count++; > if (1){ > #what must I do to return $count here. you probably don't mean "return", but merely "access" > $count_available_here_also = $count; This is it!!! You did it. It's accessable here. $count is lexically scoped to the enclosing braces, and all subordinate scopes. V $count scope ends here > }; > }; > #I do not wish to return $count here.. Add a set of braces. See example below > $but_not_here = $count; print "not accessable\n" unless defined $loop_count; { my $loop_count; print "not accessable\n" unless defined $loop_count; while () { $loop_count++; print "$loop_count\n"; if (localtime % 2) { print "$loop_count at 'random' times\n"; } } print "Loop exited having exicuted $loop_count times.\n"; } print "not accessable\n" unless defined $loop_count; It does what you'd expect. The "my" variable isn't accessable before or after the loop. (Actually, *another* one *is*. There are actually two $loop_count variables. One that we use inside the braces, and only in the braces. The other one (that we really don't use) is at a higher scope. Even though it's spelled the same, it's a different variable. Here's a good little example. It remembers the scalar between invocations, but doesn't allow it to be accessable outside the blocck that encloses the subroutine. { my $serial_num = 100_000; sub serial_num { return $serial_num; } } foreach (20..30) { printf "Next up: %d\n", serial_num(); } -- Michael R. Wolf All mammals learn by playing! [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
use strict, How to 'globalised' variable within loops. (was => Re: Question!! number of line....)
- 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 (){ > > 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 (){ 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 (){ 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 (){ 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 (){ 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]
Re: Question!! number of line....
Hiya Mark, Something like this should work: #!/usr/local/bin/perl -w use strict; my $file = "file.txt"; open (READ, "<$file") || die "Can\'t open file: $!"; my @list_of_words = ; close (READ) || die "Can\'t close file: $!"; my $counter = 1; foreach my $word (@list_of_words){ ++$counter; if ($word eq "Apple"){ my $counter2 = $counter; last; } } print "Found \'Apple\' in line $counter2"; exit; ### Each cycle in the foreach loop increases the value of $counter by 1. When the word is located, the loop exited due to the "last" command. So, if the word is in line number 5, $counter will have a value of 5. I hope this helps. Simon ## --- Mark Mclogan <[EMAIL PROTECTED]> wrote: > > 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 > orange > melon > lemon > > How I can know in that line number is the Apple word? > > Over > > MMClogan > > > _ > MSN Photos is the easiest way to share and print your photos: > http://photos.msn.com/support/worldwide.aspx > > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > = # Warmest Regards, Simon K. Chan - [EMAIL PROTECTED] "Great spirits have always encountered violent opposition from mediocre minds." -Albert Einstein __ Do You Yahoo!? Send your FREE holiday greetings online! http://greetings.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Question!! number of line....
Oops.. I made some mistake, sorry =) should be like this : ### open (FILE, ") { @lineNumber = (@lineNumber, $count) if ($_ eq "apple\n") ; $count++; } close (FILE); ### PS. if you have only 1 apple, then it valued at $lineNumber[0]; have a nice day =) - Original Message - From: "Mark Mclogan" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Saturday, December 29, 2001 5:36 AM Subject: Question!! number of line > > 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 > orange > melon > lemon > > How I can know in that line number is the Apple word? > > Over > > MMClogan > > > _ > MSN Photos is the easiest way to share and print your photos: > http://photos.msn.com/support/worldwide.aspx > > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Question!! number of line....
open (FILE, ") { if ($_ eq "apple\n") { $count++ } } close (FILE); you got the result at $count have a nice day =) - Original Message - From: "Mark Mclogan" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Saturday, December 29, 2001 5:36 AM Subject: Question!! number of line > > 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 > orange > melon > lemon > > How I can know in that line number is the Apple word? > > Over > > MMClogan > > > _ > MSN Photos is the easiest way to share and print your photos: > http://photos.msn.com/support/worldwide.aspx > > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Question!! number of line....
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 > > orange > > melon > > lemon > > > > How I can know in that line number is the Apple word? > > open FILE, 'file.txt' or die "$!\n"; > while (){ > chomp; > my $count++; $count is lexically scoped to the while loop so it will always have a value of one. > print "Apple is found in line number $count\n" if (/Apple/); > }; > close FILE; John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Question!! number of line....
- 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 > orange > melon > lemon > > How I can know in that line number is the Apple word? open FILE, 'file.txt' or die "$!\n"; while (){ chomp; my $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]
RE: Question!! number of line....
require 5; sub get_line_num { my ($file_name, $string_to_look_for) = @_; my $line_num = -1; open ( MYFILE, $file_name ) or die "Error opening file '$file_name'. Reason is <$!>"; while ( ) { if ( /$string_to_look_for/i ) { $line_num = $.; last; } } close( MYFILE ); return $line_num; } my $num = get_line_num( 'file.txt', 'apple' ); print "The word 'apple' was found in line number '$num'\n"; - [Sathish] -Original Message- From: Mark Mclogan [mailto:[EMAIL PROTECTED]] Sent: Friday, December 28, 2001 1:37 PM To: [EMAIL PROTECTED] Subject: Question!! number of line 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 orange melon lemon How I can know in that line number is the Apple word? Over MMClogan _ MSN Photos is the easiest way to share and print your photos: http://photos.msn.com/support/worldwide.aspx -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Question!! number of line....
> -Original Message- > From: Mark Mclogan [mailto:[EMAIL PROTECTED]] > Sent: Friday, December 28, 2001 4:37 PM > To: [EMAIL PROTECTED] > Subject: Question!! number of line > > > > 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 > orange > melon > lemon > > How I can know in that line number is the Apple word? Use the $. variable: perl -lne 'print $. if /Apple/' file.txt perldoc perlvar -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Question!! number of line....
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 orange melon lemon How I can know in that line number is the Apple word? Over MMClogan _ MSN Photos is the easiest way to share and print your photos: http://photos.msn.com/support/worldwide.aspx -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]