Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^3: problem with deleting a row

by Ea (Chaplain)
on Nov 22, 2013 at 11:04 UTC ( [id://1063916]=note: print w/replies, xml ) Need Help??


in reply to Re^2: problem with deleting a row
in thread problem with deleting a row

I've been writing Perl for 17 years and I'm still learning new and better ways to write code. Here's another way based on kcott's excellent answer below.
#!/usr/bin/perl -w use strict; use autodie; my ($user_file, $changed_file) = qw{test.txt test.txt.mod}; open my $in_fh, '<', $user_file; open my $out_fh, '>', $changed_file; print "Which user? "; my $user = <STDIN>; chomp $user; print $out_fh grep !/^$user,/, <$in_fh>;
This one slurps the whole file with the angle brackets, passes it through grep to filter the output which gets printed to the output file. Note the exclamation mark in front of the regex. It's the "not" operator which says you don't want the matching lines in your output.

Keep working at it. From the long list of replies, you've certainly piqued the Monks' interest.

Sometimes I can think of 6 impossible LDAP attributes before breakfast.

Replies are listed 'Best First'.
Re^4: problem with deleting a row
by Laurent_R (Canon) on Nov 23, 2013 at 16:56 UTC
    print $out_fh grep !/^$user,/, <$in_fh>;

    As far as I can say, this does not work properly. Using the example under the Perl debugger I gave above in this post Re^5: problem with deleting a row, I obtain the following with your syntax:

    DB<3> $user = "foo"; DB<10> print join " ", grep !/^$user,/, qw /foo foobar bar barfoo fo +bar foobaz /; foo foobar bar barfoo fobar foobaz DB<11> print join " ", grep /^$user,/, qw /foo foobar bar barfoo fob +ar foobaz /; DB<12>
    This apparently does not work because $user is interpreted literally as a pattern. As I said in the post mentioned above, you need to force evaluation of $user into the content of the variable (I gave in that post two possible ways of doing that) to get this to work properly.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-23 22:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found