in reply to Next Line Assigning to A Scalar

From your description it sounds like you want to retain only one color/amount per vehicle type, and that color and amount should be the last one found for each vehicle type. If that's not what you want, re-read your question, and follow-up with a more careful description of what you're after, including sample output.

If that is what you want, this should do it.

my %vehicles; while ( <DATA> ) { chomp; my( $type, $color, $amount ) = split; $vehicles{$type} = [$color,$amount]; } print for map { "$_ @{$vehicles{$_}}\n" } keys %vehicles;

Dave

Replies are listed 'Best First'.
Re^2: Next Line Assigning to A Scalar
by rmwheeler (Initiate) on Oct 30, 2014 at 15:41 UTC
    Dave thanks for the help here! This is what I would like to print out by different variable names and then use these specific variables to update a database.
    $type $color $amount -> for 1st line $type $color1 $amount1 -> for 2nd line if $type matches the 1st line $ +type $type $color2 $amount2 - for 3rd line if $type matches the 2nd $type

      So implement the algorithm as follows:

      1. Read the first line. Print it. Store its values.
      2. Read the second line. Print it if it matches the first line that you stored. Now replace your stored line with the second line.
      3. Read the third line. Print it if it matches your stored line. Store the line.
      4. Read the fourth line. Print it if it matches your stored line. Store the line.
      5. Read the fifthe line. Print it if it matches your stored line. Store the line.
      6. Read the sixth line. Print it if it matches your stored line. Store the line.
      7. Read the seventh line. Print it if it matches your stored line. Store the line.
      8. Continue these steps again and again until out of lines.

      Each time you are storing the current line so that on the next iteration you can refer back to it. Whenever you store the current line, it replaces the line you previously stored.

      You still haven't taken the time to show us sample output that the program would produce with specific sample input. Anyway, get started implementing the algorithm I've described, and let us know when you're stuck.


      Dave