in reply to Hashes and Associative Arrays
# pseudocode # PART 0 - load useful stuffs and define some useful vars use strict; use warnings; # maybe use Term::ReadLine; # a useful tool to grab user input my $filename = $ARGV[0] || 'defaultfile.txt'; my $separator = ' - '; # in your OP you split using '-' and this leave +s a blank space after the item an another before the price! my $was_modified = 0; # count total modifications make a copy of the original file with a date in the name and possibly +another exstension, just in case open the file for reading my @alllines = <$file_handle>; # in list context <> reads all the line +s (good for not huge files) # ATTENTION: you still have all newline +s at end of each record!! close file_handle or die # PART 1 - present the file to the user: show also the index of the ar +ray that we will use to modify the entry, later # better to define a sub to show the file content: sub showfile { foreach my $index( 0 .. $#alllines ) { # $#alllines is the last ind +ex (the number of) of the @alllines array chomp $alllines[$index]; # remove newlines print "[$index] $alllines[$index]\n" } modify_from_input; # call teh sub defined below: grab user input and +come back to this sub again } showfile; # call the sub # PART 2 - edit prices # grab the user input and modify the array entry. a sub is better sub modify_from_input { print "Enter the index you want to modify: (0 - $#alllines)\n"; my $input = ... # grab the input chomp $input; # remember newlines! if input is invalid warn something and call the same sub again else my ($name, $price) split $separator, $alllines[$index]; print "Enter new price for [$name] (was [$price]) or 'write' to writ +e the file:\n" my $new_price = <STDIN>; chomp $new_price; # validate $new_price ? if new_price is empty -> remove the item? -> splice the array -> sho +wfile return if $new_price eq 'write'; # break the loop of the 2 subs and +go to the end of program # modification! $was_modified++; $alllines[$index] = $name.$separator.$new_price; showfile; # we go back in show mode } # PART 3 - modify the file: we arrive here only if user enter 'write' +while in the modify_from_input sub open my $write, '>', filename or die # here we are rewriting the orig +inal file print $write "$_\n" for @alllines; if $was_modified print "$was_modified modifications were applied\n" else print "no modifications\n" close $write or die; print "$filename succesfully written\n"; exit 0;
|
|---|