Problem rewinding the __DATA__ filehandle
Dear Perlers, I am trying to print the matching lines using __DATA__ filehandle and for the very first time it prints the desired lines, but as soon as I rewind it using seek, it is printing lines from the very beginning which is not the desired result (as it prints from the start). I want to rewind only the __DATA__ part. Please help to print the matching lines using approach 2 and 3 as well. [code] use strict; use warnings; #Approach 1 #Printing lines 2 through 5 using range operator while (DATA) { print if 2 .. 5; } print Info: The position of DATA file handle is , tell DATA, \n; seek DATA, 0, 0; print Info: The position of DATA file handle is , tell DATA, \n; #Approach 2 #Printing lines 2 through 5 using counter. my $counter = 1; while (DATA) { print if $counter = 2 $counter = 5; $counter++; } print Info: The position of DATA file handle is , tell DATA, \n; seek DATA, 0, 0; print Info: The position of DATA file handle is , tell DATA, \n; #Printing lines 2 through 5 using $. while (DATA) { print if $. = 2 $. = 5; } __DATA__ India Srilanka USA Nepal France UK Australia New Zealand [/code] [output] Srilanka USA Nepal France Info: The position of DATA file handle is 777 Info: The position of DATA file handle is 0 use warnings; #Approach 1 #Printing lines 2 through 5 using range operator Info: The position of DATA file handle is 777 Info: The position of DATA file handle is 0 [/output] Thank you. best, Shaji --- Your talent is God's gift to you. What you do with it is your gift back to God. ---
Re: Problem rewinding the __DATA__ filehandle
On Oct 25, 2013, at 4:46 PM, Shaji Kalidasan wrote: Dear Perlers, I am trying to print the matching lines using __DATA__ filehandle and for the very first time it prints the desired lines, but as soon as I rewind it using seek, it is printing lines from the very beginning which is not the desired result (as it prints from the start). I want to rewind only the __DATA__ part. As you have discovered, the special file handle DATA is opened to the source file for your program. When the compiler encounters either of the following lines in your source file: __END__ __DATA__ it stops reading the source file and leaves the file handle open at that point. The first time you read the DATA file handle, you get the line after the '__DATA__' line. But when you rewind the file handle, it is positioned at the beginning of the file, and you get to read your program. You have two choices (at least): 1. Save the position of the file handle when you enter the program: my $data_pos = tell(DATA); Then reposition the file to this point instead of at the beginning of the file: seek( DATA, $data_pos, 0 ); 2. After rewinding the file to the beginning with seek(DATA,0,0), read the file until you encounter the '__DATA__' line. Then start reading the data lines. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Problem rewinding the __DATA__ filehandle
On Fri, Oct 25, 2013 at 05:14:23PM -0700, Jim Gibson wrote: On Oct 25, 2013, at 4:46 PM, Shaji Kalidasan wrote: Dear Perlers, I am trying to print the matching lines using __DATA__ filehandle and for the very first time it prints the desired lines, but as soon as I rewind it using seek, it is printing lines from the very beginning which is not the desired result (as it prints from the start). I want to rewind only the __DATA__ part. As you have discovered, the special file handle DATA is opened to the source file for your program. When the compiler encounters either of the following lines in your source file: __END__ __DATA__ it stops reading the source file and leaves the file handle open at that point. The first time you read the DATA file handle, you get the line after the '__DATA__' line. But when you rewind the file handle, it is positioned at the beginning of the file, and you get to read your program. You have two choices (at least): 1. Save the position of the file handle when you enter the program: my $data_pos = tell(DATA); Then reposition the file to this point instead of at the beginning of the file: seek( DATA, $data_pos, 0 ); 2. After rewinding the file to the beginning with seek(DATA,0,0), read the file until you encounter the '__DATA__' line. Then start reading the data lines. The first choice is the correct one. Ideally, you should use the SEEK_SET constant from Fcntl. perldoc Fcntl for details. And for your third approach, you need C $. = 0; -- Paul Johnson - p...@pjcj.net http://www.pjcj.net -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Problem rewinding the __DATA__ filehandle
Dear Jim, Awesome, thanks a bunch for your detailed explanation. It works like a champ. best, Shaji --- Your talent is God's gift to you. What you do with it is your gift back to God. --- On Saturday, 26 October 2013 5:44 AM, Jim Gibson jimsgib...@gmail.com wrote: On Oct 25, 2013, at 4:46 PM, Shaji Kalidasan wrote: Dear Perlers, I am trying to print the matching lines using __DATA__ filehandle and for the very first time it prints the desired lines, but as soon as I rewind it using seek, it is printing lines from the very beginning which is not the desired result (as it prints from the start). I want to rewind only the __DATA__ part. As you have discovered, the special file handle DATA is opened to the source file for your program. When the compiler encounters either of the following lines in your source file: __END__ __DATA__ it stops reading the source file and leaves the file handle open at that point. The first time you read the DATA file handle, you get the line after the '__DATA__' line. But when you rewind the file handle, it is positioned at the beginning of the file, and you get to read your program. You have two choices (at least): 1. Save the position of the file handle when you enter the program: my $data_pos = tell(DATA); Then reposition the file to this point instead of at the beginning of the file: seek( DATA, $data_pos, 0 ); 2. After rewinding the file to the beginning with seek(DATA,0,0), read the file until you encounter the '__DATA__' line. Then start reading the data lines. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Problem rewinding the __DATA__ filehandle
Jim Gibson wrote: On Oct 25, 2013, at 4:46 PM, Shaji Kalidasan wrote: Dear Perlers, I am trying to print the matching lines using __DATA__ filehandle and for the very first time it prints the desired lines, but as soon as I rewind it using seek, it is printing lines from the very beginning which is not the desired result (as it prints from the start). I want to rewind only the __DATA__ part. As you have discovered, the special file handle DATA is opened to the source file for your program. When the compiler encounters either of the following lines in your source file: __END__ __DATA__ it stops reading the source file and leaves the file handle open at that point. The first time you read theDATA file handle, you get the line after the '__DATA__' line. But when you rewind the file handle, it is positioned at the beginning of the file, and you get to read your program. You have two choices (at least): 1. Save the position of the file handle when you enter the program: my $data_pos = tell(DATA); DATA is the same as readline( DATA ) which will not work with tell(). It should be just: my $data_pos = tell DATA; John -- Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction. -- Albert Einstein -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Problem rewinding the __DATA__ filehandle
Dear Paul, Many thanks for your explanation on Approach 3 Here is the working code Courtesy : 1) Jim Gibson (For Approach 1 and 2) 2) Paul Johnson (For Approach 3) 3) John W Krahn (I figured it out myself, thanks for emphasizing the fact to greater audience) [code] use strict; use warnings; my $data_position = tell DATA; #Approach 1 #Printing lines 2 through 5 using range operator while (DATA) { print if 2 .. 5; } print Info: The position of DATA file handle is , tell DATA, \n; seek DATA, $data_position, 0; print Info: The position of DATA file handle is , tell DATA, \n; #Approach 2 #Printing lines 2 through 5 using counter. my $counter = 1; while (DATA) { print if $counter = 2 $counter = 5; $counter++; } print Info: The position of DATA file handle is , tell DATA, \n; seek DATA, $data_position, 0; print Info: The position of DATA file handle is , tell DATA, \n; #Approach 3 #Printing lines 2 through 5 using $. $. = 0; while (DATA) { print if $. = 2 $. = 5; } __DATA__ India Srilanka USA Nepal France UK Australia New Zealand [/code] [output] Srilanka USA Nepal France Info: The position of DATA file handle is 855 Info: The position of DATA file handle is 792 Srilanka USA Nepal France Info: The position of DATA file handle is 855 Info: The position of DATA file handle is 792 Srilanka USA Nepal France [/output] best, Shaji --- Your talent is God's gift to you. What you do with it is your gift back to God. --- On Saturday, 26 October 2013 8:56 AM, John W. Krahn jwkr...@shaw.ca wrote: Jim Gibson wrote: On Oct 25, 2013, at 4:46 PM, Shaji Kalidasan wrote: Dear Perlers, I am trying to print the matching lines using __DATA__ filehandle and for the very first time it prints the desired lines, but as soon as I rewind it using seek, it is printing lines from the very beginning which is not the desired result (as it prints from the start). I want to rewind only the __DATA__ part. As you have discovered, the special file handle DATA is opened to the source file for your program. When the compiler encounters either of the following lines in your source file: __END__ __DATA__ it stops reading the source file and leaves the file handle open at that point. The first time you read theDATA file handle, you get the line after the '__DATA__' line. But when you rewind the file handle, it is positioned at the beginning of the file, and you get to read your program. You have two choices (at least): 1. Save the position of the file handle when you enter the program: my $data_pos = tell(DATA); DATA is the same as readline( DATA ) which will not work with tell(). It should be just: my $data_pos = tell DATA; John -- Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction. -- Albert Einstein -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/