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

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; }

Replies are listed 'Best First'.
Re^4: problem with deleting a row
by Laurent_R (Canon) on Nov 21, 2013 at 19:03 UTC
    Why are you using square brackets around the $user variable? Don't forget to chomp $user.
Re^4: problem with deleting a row
by brianMonk (Initiate) on Nov 21, 2013 at 19:56 UTC
    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