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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re^3: Practicing with files
by davido (Cardinal) on Apr 01, 2014 at 19:59 UTC

    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

      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
Re^3: Practicing with files
by 2teez (Vicar) on Apr 01, 2014 at 20:01 UTC

    Hi Jabox,
    ..No that isn't what I'm looking for, and this is a different practice from the other ones...

    Going through some the suggestions given to you on the previous post outlined by davido, it obvious the answer to the question you asked has been given, all you need do is just adaptation.

    Something like so:

    use warnings; use strict; use Data::Dumper; chomp( my @data = <DATA> ); my %fruits = map { lc($_) => 1 } @data; print "What do you want to remove?: ", join " " => @data, "\n", "Input: "; chomp( my $ans = lc(<STDIN>) ); if ( $fruits{$ans} ) { print "Seen ", $ans, $/; delete $fruits{$ans}; } else { print $ans, " that item doesn't exist", $/; } print Dumper \%fruits; __DATA__ Apple Orange Grape
    Which when I checked, looked almost identical to the solution given by toolic in Re: Push,pop, and splice!.
    Please check again.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me