Jabox has asked for the wisdom of the Perl Monks concerning the following question:
Good day everyone,
I'm practicing perl still and I made this code to play around with file writings and readings
The only part I don't get how to do is how to not make my input read line by line but all together?
Example:
What do you want to remove? : Apple Orange Grape
input:"grape", response:"that item doesn't exist"
input:"grape", response:"that item doesn't exist"
input:"grape", the item grape has been removed!
Basically it compared each item for each STDIN and since grape was the $item2 it reads it on the 3rd input
The other code I did worked fine but I don't know how to do it for the if condition. So if it exist, then print it and remove / write over old file.
sub depo { open (IFILE, "<", "Inventory.txt") or die "Can not open file\n +"; my @inv = (<IFILE>); close (IFILE); while (<@inv>) { chomp $_; print ucfirst . "\n"; } my $ans = lc(<STDIN>); chomp $ans; print "Say 'menu' to go back to menu.\n"; while (<@inv>) { chomp $_; if ($_ =~ m/$ans/) { open (SFILE, ">>", "Storage2.txt") or die "Can + not open file\n"; print SFILE "$ans\n"; close (SFILE); open (WFILE, ">", "Inventory.txt") or die "Can + not open file\n"; while (<@inv>) { print WFILE "$_\n" unless /$ans/; } print "You have stored $ans\n"; close (WFILE); &depo; } elsif ($ans eq "menu") { &menu; } else { print "Item does not exist in Inventory!\n"; &depo; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Practicing with files
by davido (Cardinal) on Apr 01, 2014 at 18:41 UTC | |
|