Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^3: While loop not printing anything after using grep

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


in reply to Re^2: While loop not printing anything after using grep
in thread While loop not printing anything after using grep

See my previous node again:

Else clause is alwayse executed, why (should only be executed if 'Friday' is not found in 'input.txt')?

You haven't used the <> operator, as in grep {/Monday/} <$in_file>.

if ($. == 1) {

If you use seek, you need to reset $. as I showed.

Empty block in the if statement; how can I avoid this?

You can invert the condition in the if, as in if ( !( ... ) ) or if ( not ... ) (just be aware of Operator Precedence and Associativity). Perl also offers the equivalent unless (...) {...}, but IMHO sometimes if (not ... is still more readable than unless (....

As far as I understand it, in Perl you first need to create a new file with the desired output then rename that file to the original name. So in my case that would be with the rename function

Yes, that is one way to do it - if you don't mind me plugging my own module, see File::Replace.

Log In?
Username:
Password:

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

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

    No recent polls found