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.)


In reply to Re: While loop not printing anything after using grep (updated) by haukex
in thread While loop not printing anything after using grep by skjeiu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.