Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: problem with deleting a row

by brianMonk (Initiate)
on Nov 21, 2013 at 17:10 UTC ( [id://1063757]=note: print w/replies, xml ) Need Help??


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

Ea, thanks very much for your code, it works. However, I have to make a script, not command line statement. And I done this:
#!/usr/bin/perl -w open( FILE, "<", "test.txt") or die "cannot open > test.txt: $!"; while (<FILE>) { @array = <FILE>; @a = grep (/^lb348,/, @array); close FILE; print $a; }
But it does not work. Sorry, I am new to programming.

Replies are listed 'Best First'.
Re^3: problem with deleting a row
by Laurent_R (Canon) on Nov 21, 2013 at 18:37 UTC

    This:

    while (<FILE>) { @array = <FILE>;
    is just wrong. Either you read the file line by line with a while loop, or you "slurp" it into an array, but don't do both. The you are constructing a @a array and print $a, a scalar variable. The following pragmas:
    use strict; use warnings;
    would have picked up these errors.

Re^3: problem with deleting a row
by Ea (Chaplain) on Nov 22, 2013 at 11:04 UTC
    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.
      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.

Re^3: problem with deleting a row
by brianMonk (Initiate) on Nov 21, 2013 at 18:42 UTC
    Well I managed to do this. The code returns every line except the one that the user wants to delete entering first username. However, if in my test.txt file two usernames start with the same letter (for example, la123 and lv444), the program will remove both lines, regardless user input that is for example la123.
    $file = "test.txt"; print "Enter the username you want to remove from the information.txt +file\n"; $user = <STDIN>; open( FILE, $file) or die "cannot open > test.txt: $!"; while (<FILE>) { chomp; @array = <FILE>; @a = grep (!/^[$user]/, @array); print @a; }
      Why are you using square brackets around the $user variable? Don't forget to chomp $user.
      Lauren, if I remove square brackets around $user, the program will just output every line refusing to perform grep function.

        if I remove square brackets around $user, the program will just output every line refusing to perform grep function.

        This is because you need to force the evaluation of the $user variable. This is an example under the Perl debugger of one possible way to do it:

        DB<1> @array = qw /foo foobar bar barfoo fobar foobaz /; DB<2> x @array; 0 'foo' 1 'foobar' 2 'bar' 3 'barfoo' 4 'fobar' 5 'foobaz' DB<3> $user = "foo"; DB<4> @a = grep /^\Q$user\E/, @array; DB<5> print "@a"; foo foobar foobaz DB<7> print join " ", grep !/^\Q$user\E/, @array; bar barfoo fobar
        But using square brackets does not work as expected because it is defining a character class:
        DB<8> print join " ", grep !/^[$user]/, @array; bar barfoo DB<9> print join " ", grep /^[$user]/, @array; foo foobar fobar foobaz
        On the other hand, using ${user} instead of $user will be sufficient to get it to work properly:
        DB<27> print join " ", grep /^${user}/, @array; foo foobar foobaz DB<28> print join " ", grep !/^${user}/, @array; bar barfoo fobar

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-23 09:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found