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

I'm totally unclear on what you want to have happen. You want to force the user to type 'grape' three times before the program removes grape once? Seems to be an unlikely need.

You want to look at line one of the file, take user input, and if line one matches the user input remove it, then move on to the next line, and so on?

  1. File: "Apple\nBananna\nGrape". Script looks at "Apple". User inputs "Grape". Grape doesn't match Apple. Move on.
  2. File: "Apple\nBananna\nGrape". Script looks at Bananna. Uer inputs Grape. Grape doesn't match Bananna. Move on.
  3. File: "Apple\nBananna\nGrape". Script looks at 'Grape'. User inputs 'Grape'. Grape matches Grape. Remove Grape from file.
  4. File: "Apple\nBananna\n". Script finds no more lines in the file; close the file and exit.

Is that what you're looking for? As I mentioned in a previous post, a single flat newline delimited file where each line can be of arbitrary length is not well suited to this task. The best I can suggest is to open a second file for output. At step one, write Apple to the second file. At step two, write Bananna to the second file. At step three, do not write Grape to the second file. At step four, close the output file, and rename it to take the place of the original input file.

In this way, you're essentially re-writing the original file by making a copy that has whatever modifications you need, and then replacing the original with the copy.

A newline-delimited file where lines may be of arbitrary length is not suited for in-place editing. You almost always have to write out a copy.


Dave

Replies are listed 'Best First'.
Re^4: Practicing with files
by Jabox (Sexton) on Apr 01, 2014 at 20:20 UTC

    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.

      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.