Re: Problem iterating over diamond (whileFILE)
Nitpick - the diamond operator is . INFILE would be use of the readline operator. -- Peter Scott http://www.perldebugged.com/ *** NEW *** http://www.perlmedic.com/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Problem iterating over diamond (whileFILE)
On 29 Sep 2004 14:06:33 -, Peter Scott [EMAIL PROTECTED] wrote: Nitpick - the diamond operator is . INFILE would be use of the readline operator. Thanks for making it precise Peter. Glad that I learnt that. Forgive me for my limited Perl vocabulary. Regards, Edward WIJAYA SINGAPORE -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Problem iterating over diamond (whileFILE)
Hi, Suppose I have a data file that contain these lines: output1 output2 when I run the code below with: perl mycode.pl -f datafile it gives: Trial 1 output1 output2 Trial 2 instead of: Trial 1 output1 output2 Trial 2 output1 output2 Can we actually loop over the 'while diamond'? Please kindly advice how can I overcome this problem. Thanks so much for your time. Regards, Edward WIJAYA SINGAPORE __BEGIN__ use strict; use warnings; use Getopt::Std; our $opt_f; getopts('f:'); open INFILE, $opt_f or die $0: Can't open file $opt_f: $!; my $trial = 2; #What's wrong with running a for loop over the 'while' here? for ( my $t = 1 ; $t = $trial ; $t++ ) { print Trial , $t, \n; while (INFILE) { print; } } __END__ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Problem iterating over diamond (whileFILE)
Can we actually loop over the 'while diamond'? Please kindly advice how can I overcome this problem. I don't know if I'm skilled enough to answer this yet, but shouldn't you be using: my @myarray = FILE; foreach (@myarray) { do stuff } Thanks so much for your time. Regards, Edward WIJAYA SINGAPORE __BEGIN__ use strict; use warnings; use Getopt::Std; our $opt_f; getopts('f:'); open INFILE, $opt_f or die $0: Can't open file $opt_f: $!; my $trial = 2; #What's wrong with running a for loop over the 'while' here? for ( my $t = 1 ; $t = $trial ; $t++ ) { print Trial , $t, \n; while (INFILE) { print; } } __END__ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Problem iterating over diamond (whileFILE)
Gavin Henry said: Can we actually loop over the 'while diamond'? Please kindly advice how can I overcome this problem. I don't know if I'm skilled enough to answer this yet, but shouldn't you be using: Nope, sorry. It helps if I *actually* read the rest of your e-mail. Sorry. my @myarray = FILE; foreach (@myarray) { do stuff } Thanks so much for your time. Regards, Edward WIJAYA SINGAPORE __BEGIN__ use strict; use warnings; use Getopt::Std; our $opt_f; getopts('f:'); open INFILE, $opt_f or die $0: Can't open file $opt_f: $!; my $trial = 2; #What's wrong with running a for loop over the 'while' here? for ( my $t = 1 ; $t = $trial ; $t++ ) { print Trial , $t, \n; while (INFILE) { print; } } __END__ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Problem iterating over diamond (whileFILE)
Edward Wijaya wrote: Hi, Suppose I have a data file that contain these lines: output1 output2 when I run the code below with: perl mycode.pl -f datafile it gives: Trial 1 output1 output2 Trial 2 instead of: Trial 1 output1 output2 Trial 2 output1 output2 Can we actually loop over the 'while diamond'? Please kindly advice how can I overcome this problem. Thanks so much for your time. Regards, Edward WIJAYA SINGAPORE __BEGIN__ use strict; use warnings; use Getopt::Std; our $opt_f; getopts('f:'); open INFILE, $opt_f or die $0: Can't open file $opt_f: $!; my $trial = 2; #What's wrong with running a for loop over the 'while' here? for ( my $t = 1 ; $t = $trial ; $t++ ) { print Trial , $t, \n; while (INFILE) { print; } } When you reach the end of file on INFILE (during Trial 1), further reads will just return undef. You need to either close and reopen the file, or rewind the file using seek() before you can re-read the data. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Problem iterating over diamond (whileFILE)
Thanks a lot for your reply Bob. but can you be more specific: You need to either close and reopen the file, or rewind the file using seek() before you can re-read the data. I tried seek() following perldoc like this: __BEGIN__ for ( my $t = 1 ; $t = $trial ; $t++ ) { for ( $curpos = tell(INFILE); $_ = INFILE; $curpos = tell(INFILE)) { print Trial , $t, \n; while (INFILE) { print;} } seek(INFILE, $curpos, 0); } but doesn't seem to work. Also tried to close and 'reopen'. Doesn't seem to work as well. __BEGIN__ for ( my $t = 1 ; $t = $trial ; $t++ ) { print Trial , $t, \n; while (INFILE) { print;} } close INFILE; open INFILE; #is this how you reopen? __END__ Hope you don't mind to elaborating it. Thanks again. Regards Edward WIJAYA -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Problem iterating over diamond (whileFILE)
Edward Wijaya wrote: Thanks a lot for your reply Bob. but can you be more specific: You need to either close and reopen the file, or rewind the file using seek() before you can re-read the data. What I mean is: for ( ...blah... ) { seek(INFILE, 0, 0); # --- rewind file back to start while (INFILE) { ... } } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Problem iterating over diamond (whileFILE)
Many many thanks Bob! Glad to know I can do it in one-line. for ( ...blah... ) { seek(INFILE, 0, 0); # --- rewind file back to start while (INFILE) {... } } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Problem iterating over diamond (whileFILE)
Hi Edward, On Tue, 28 Sep 2004 11:20:39 -0400, Bob Showalter [EMAIL PROTECTED] wrote: Edward Wijaya wrote: Thanks a lot for your reply Bob. but can you be more specific: You need to either close and reopen the file, or rewind the file using seek() before you can re-read the data. SNIP Try this: #!/usr/bin/perl use strict; use warnings; use Getopt::Std; our $opt_f; getopts('f:'); my $trial = 2; for ( my $t = 1 ; $t = $trial ; $t++ ) { print Trial $t\n; open INFILE, $opt_f or die $0: Can't open file $opt_f: $!; while (INFILE) { print; } } As a note, in the above code, you used a line: print Trial , $t, \n; But I changed it to: print Trial $t\n; The fun part of using double quotes ( ) is that Perl will interpolate (think translate) any variables inside those double quotes that it finds! Helps with reducing your typing AND has the added benifit of making your code more readable! When you use 'open' twice on the same file handle, it will first close that file handle, then RE-open it with the input ready to go at the beginning again! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response