Hey all, Brand new to Perl and am having a great time, but am stumped with this problem. I have an inventory list broken up like this:
YEAR MAKE MODEL PRICE 2003 abc rel 999 1999 hds sdf 100 2010 kls pol 1400
What I am doing is storing that list as an array:
@inventory=('2003 abc rel 999', '1999 hds sdf 100', '2010 kls pol 1400');
I want to run the program and take a user input that says YEAR, MAKE, PRICE and sort it accordingly. Here's what I've tried, but was not right was it is separating the columns and storing them together as oppose to sorting by column while keeping the rows intact.
$lookfor=<STDIN>; chop($lookfor); foreach $inv (@inventory) { ($YEAR,$MAKE,$MODEL,$PRICE)=split(/ /, $inv); push (@year,$YEAR); push (@make, $MAKE); push (@price, $PRICE); } if ($lookfor =~ /year/i){ @sorted = sort @year; print join "\n",@sorted; print "\n"; } if ($lookfor =~ /make/i){ @sorted = sort @make; print join "\n", @sorted; print "\n"; }
Any tips on how to make this work would be great!

In reply to Inventory List Sorting Question by fufaso

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.