Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Next Line Assigning to A Scalar

by rmwheeler (Initiate)
on Oct 29, 2014 at 19:56 UTC ( [id://1105543]=perlquestion: print w/replies, xml ) Need Help??

rmwheeler has asked for the wisdom of the Perl Monks concerning the following question:

Hello, Can someone help me with some direction? I'm trying to write a Perl script that will read each line and if the "Car" string matches the next line string "Car" it will assign "Black" to a variable and then do the same for string "Car" Yellow as a different variable and so forth.
Sample Data: Car Red 100 Car Black 100 Car Yellow 100 Bus Green 400 Truck Blue 300
open (FILE, 'test.txt') or die "$!\n"; my @line = <FILE>; foreach my $line (@line) { chomp $line; my($car, $color, $amount) = split(/ /, $line); print "$car $color $amount\n"; }
Thanks!

Replies are listed 'Best First'.
Re: Next Line Assigning to A Scalar
by davido (Cardinal) on Oct 29, 2014 at 21:04 UTC

    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

      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

Re: Next Line Assigning to A Scalar
by toolic (Bishop) on Oct 29, 2014 at 20:07 UTC
    • Your data looks like it's on one line. If not, edit your post and place the data in "code" tags (Writeup Formatting Tips).
    • Read perlintro, then post some code you've tried with actual and expected output.
      I updated the post with the code that I've started out with. I'm just not sure how to go about the rest.
        Ok, so you gave us runnable code and nicely formatted data, but you still didn't show your desired output. I don't understand from your text description what you want.
Re: Next Line Assigning to A Scalar
by GotToBTru (Prior) on Oct 29, 2014 at 20:38 UTC

    What form will the output take? Sounds like you want to store color alternatives. How will you use those alternatives? Will you need to sort them? Can there be more than one black car? Do you need to store the number after the color somewhere?

    Your post title talks about assigning to a scalar, but I have trouble imagining that as a useful thing. Perhaps use a hash instead? Put the following right after your print statement (inside the foreach loop):

    $price_by_type_and_color{$car}{$color} = $amount;

    What kind of vehicles are available?

    printf "%s\n",$_ for sort keys %price_by_type_and_color; Output: Bus Car Truck

    Updates: minor typos and omissions fixed

    1 Peter 4:10
      Thanks so much for the assistance. I guess this is the best way of explaining it since I have no output.
      Variables ========= $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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1105543]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (12)
As of 2024-04-23 14:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found