in reply to Re: Hashes and Associative Arrays
in thread Hashes and Associative Arrays

I am updating it now and I will post here aswell with the new code. The text file says this exactly Hamburger - 1.79 Cheeseburger - 2.00 Fries - 1.50

I took a different approach and had perl read the file print what it said then take that and put it into a hash now I need to take user input and change the hash. My guess would be to use a foreach loop to run through each line of the uploaded hash and change it.

use strict; use warnings; my $file = 'gp1data.txt'; open my $info, $file || die "Could not open $file: $!"; while( my $line = <$info>) { chomp($line); print $line; (my $word1, my $word2) = split /-/, $line; $hash{word1} = $word2; } while ( my ($k, $v) = each $hash){ print "key $k => $v\n"; } <>;

Replies are listed 'Best First'.
Re^3: Hashes and Associative Arrays
by GrandFather (Saint) on Jan 27, 2016 at 03:33 UTC

    You haven't yet taken your programmer hat off and put a user hat on. Until you do that and specify how the user will interact with your script, you can't write the script!

    Is your input really all on one line? Maybe you need to put your sample data in code tags (you can edit your node to do that).

    Premature optimization is the root of all job security

      I am sort of confused, the user will open the program and items and their prices will be printed then the user will be asked to change the prices of the items that were printed. These items should come from a text file.

        OK, there's the specification. Now all you need to do is write the code to do that. One way to work through it is write three different tiny scripts that each does one of the tasks. Mock up the data that may have been created by a previous step so you can focus on how to manage the current step.

        Mocking up the data may be just hard wiring data into a hash or array depending on what you think may be the best way to work with the data. The important thing is to focus on just the current part of the overall job. Often you will find that one part of the task needs the data to be a certain way and you need to go back and modify a previous step, but that's OK because you are still focusing on whatever the current step is.

        Premature optimization is the root of all job security