in reply to Re^3: Practicing with files
in thread Practicing with files

Gah I'm not so good with explaining but that is what it is doing and I do not want that to happen. All 3 of those words are in 1 file. I want to be able to just type in Grape once and it will remove what I typed in. Right now I can only remove the 3rd line if I type it in 3 times. It's all listed I just want to type it in once no matter which value it is and it will know.

From the snippet above, when I type in grape the "if" only goes through one $_ which the first one is apple.I have to type it in 3 times for it to detect the 3rd $_ which is Grape to even remove it.

elsif (($choice eq "delete") || ($choice eq 2)) { open (STORFILE ,"<", "Storage2.txt") or die "Can not o +pen file\n"; my @storf = <STORFILE>; close (STORFILE); open (my $stor2,">", "Storage2.txt") or die "Can not o +pen file\n"; print "What items would you like to delete?\n"; while (<@storf>) { chomp $_; print ucfirst . "\n"; } my $ans = lc (<STDIN>); chomp $ans; while (<@storf>) { print $stor2 "$_\n" unless /$ans/; } close ($stor2); &stor; }

This one works fine, but that's for just adding and deleting anything into the file.

The snippet on the top, I want to make it read, IF it exist, then do this. BUT it requires more than 1 input till it finally detects what I want to read and remove.

As for the snippet here, I just type it in and it removes what ever it matches

I don't know how else to say it but, for the top snippet I want to type grape only once and read all 3 lines. But since I added the if command it reads only one line ONCE and that is it which is not what I want.

Replies are listed 'Best First'.
Re^5: Practicing with files
by GrandFather (Saint) on Apr 02, 2014 at 01:33 UTC

    If you can't explain it to people who can make educated guesses at what you might mean, how do you expect to explain it to Perl that hardly makes any guesses at all (even bearing in mind DWIM)?

    Part of being a good programmer is having a clear idea of what you want your program to achieve and a plan for how you are going go about achieving it. Trying to explain what you want to do to other people is a good way of figuring both those goals out (the Teddy Bear / Rubber Duck effect).

    Perl is the programming world's equivalent of English
      ++GrandFather very well said and explained.
      Even if this remember me a big monk of the past ages: Augustine of Hippo told: "What then is time? If no one asks me, I know what it is. If I wish to explain it to him who asks, I do not know." in Confessions.

      L*
      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      I don't know how else to say it if what I said don't even make sense to others.

      But let me try to give another example

      Right now the list of lines in a file reads

      Apple

      Orange

      Grape

      SO, if I type grape in once, it says it doesn't exist according to the snippet on the first post.

      It only works if I type grape in 3 times to get to the 3rd line, it's all in the same file

      I don't want to type the same word 100 times to check all 100 lines before I get to the one I want*******

      I want to type in only 1 word, reads every line, if it exist, then remove it and add to the other file.

      I really hope this is more understandable.

        I don't want the if $_ stop only on one line, I want it to loop every line before it goes to else

        I found my solution, had to add this to match it more than once.

        if ($_ =~ m/([$ans]+)/) {