Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: While loop not printing anything after using grep (updated)

by haukex (Archbishop)
on Dec 29, 2020 at 11:33 UTC ( [id://11125926]=note: print w/replies, xml ) Need Help??


in reply to While loop not printing anything after using grep

Welcome to the Monastery, skjeiu. Please note the advice in How do I post a question effectively?: At the very least, please use <code> tags to format your code. Also, you posted this question in the incorrect section; I have moved it to Seekers of Perl Wisdom for you.

if (grep{/Friday/} $in_file)

This is unlikely to be the code you're actually running, as this would try to match the pattern against the filehandle stored in the variable, instead of the contents of the file. Instead, I assume your code looks like if (grep{/Friday/} <$in_file>), as that would explain the problem you're having: in list context, the <$filehandle> operator (see I/O Operators in perlop) reads all the lines from the file and returns them, this is what grep is matching against, but once you've read everything from the file, the second <> operator won't return anything.

There are several possible ways to work around this. For example, if the file will always be small enough to comfortably fit into memory, you could read the contents of the file into an array and then loop over that with grep and foreach as many times as you like. Another approach would be to integrate everything into one while loop, i.e. move the /Friday/ match there. If you could explain more about what your input files look like and what you're actually trying to do, I'm sure we could suggest some approaches fitting to your application.

Update:

My ultimate gall here is to search for the string 'Friday' in the file 'input.txt', if 'Friday' is missing, add'Friday' to 'input.txt on line 2. The content of 'input.txt is: Monday Sunday

Sorry, I must have missed this on the first read because of the formatting (unless you edited your node? please mark your updates when editing). Anyway, please also use <code> tags for sample input and output. Since you haven't shown your expected output, it's a little hard to guess what you mean - do you want the contents of the second line to be replaced, the string "Friday" to be appended, or do you want a new line to be inserted? And does the length of the input file matter at all?

Anyway, your approach is a good start. Assuming your files aren't too long, then one way to do what you want would be to rewind the input file to the beginning with seek: insert seek $in_file, 0, 0 or die "seek: $!"; after the grep and before the while to start reading the file from the beginning again. You should then be able to use this as the basis to do what you want. (Note that this does have one disadvantage: it doesn't reset the special line counter variable $., in case you were planning to use that in your while loop - it is possible to do this manually via $.=0 right after the seek.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11125926]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-24 18:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found