Home: Perl Programming Help: Beginner:
open and close file before each while-looping through file?



Drinc3
New User

Sep 12, 2013, 8:47 AM


Views: 1540
open and close file before each while-looping through file?

I recognized that in the following code the second while-loop doesn't take in the file once more. With an additional opening and closing it seems to work well. Is that intented?
[perl]
open ($file, "< filename.txt");

while (<$file>) {
m/(keyword) (.+)/;
push @keywords, $2;
}

while (<$file>) {
m/(other_keyword) (.+)/;
push @other_keywords, $2;
}
print @keywords;
print @other_keywords;
[/perl]

EDIT: Why isn't the code properly formatted?
EDIT2: Why is it I can't find the button for watching a thread? That is kind of embarrassing.


(This post was edited by Drinc3 on Sep 12, 2013, 8:57 AM)


FishMonger
Veteran / Moderator

Sep 12, 2013, 10:17 AM


Views: 1528
Re: [Drinc3] open and close file before each while-looping through file?


Quote
EDIT: Why isn't the code properly formatted?

You need to use the code tags, which is the button at the bottom-left of the input text box.

After the first while loop the filepointer is positioned at the end of the file. If you want to loop over the contents a second time, you need to either use the seek function to reset the file pointer, or close and reopen the file. Between these 2 options, using seek would be better.

However, a better option would be to rework your logic and only loop over the file once.


(This post was edited by FishMonger on Sep 12, 2013, 10:17 AM)


Laurent_R
Veteran / Moderator

Sep 12, 2013, 2:38 PM


Views: 1522
Re: [Drinc3] open and close file before each while-looping through file?

As already said by Fishmonger, the first while loop gets you to the end of the file, the second while loop sees an end of file and exits immediately. You could close and reopen the file, which is one way to get back to the start of the file, or use the seek function (something like: 'seek $file, 0, 00;').

But it would be far better to read the file only once and make the two checks in one go:


Code
use strict;            # always use strict 
use warnings; # always use warnings
my (@keywords, @other_keywords);
open my $file, "<", "filename.txt" or die "Could not open filename.txt $!"; # always check the success of a file opening
while (<$file>) {
push @keywords, $2 if /(keyword) (.+)/;
push @other_keywords, $2 if /(other_keyword) (.+)/;
}
# etc.


Please note that, in addition to rearranging the code to do the two regex checks for each line immediately, I have also slightly changed the logic, making the push onto the array conditional to the regex success.


Drinc3
New User

Sep 12, 2013, 11:46 PM


Views: 1510
Re: [Laurent_R] open and close file before each while-looping through file?

Thanks a lot Fishmonger and Laurent_R!

I will use your advice Wink!

PS I got email for my topic, so I think I was "watching" it. Which is curious, because I didn't find the topic in my watchlist, which was empty.

PPS And what about code-highlighting? In the following example it doesn't work as expected.


Code
[perl] print "blubber\n";[/perl]



FishMonger
Veteran / Moderator

Sep 13, 2013, 6:46 AM


Views: 1505
Re: [Drinc3] open and close file before each while-looping through file?

Personally, I find that highlighting "feature" distracting and I never felt the need to use it.

Unfortunately, this site doesn't have an active moderator or maintainer, so getting the [perl] .. [/perl] tag fixed probably won't happen anytime soon.

But, please do use the code tags in the future so that the code formatting is retained. That is the most important tag when posting any type of coding question.


Drinc3
New User

Sep 13, 2013, 6:57 AM


Views: 1503
Re: [FishMonger] Highlighting

Thanks for the info. I will use only

Code
Thats plain code only with some bold mark.

in my future posts.
Have a nice weekend Smile!