If you had used strict, an error message would have pointed out that you typo'd $seach and probably meant $search in your last print statement.

Also, you might want to consider a different delimiter character than ":". The colon appears in many book titles, so what will happen when you eventually encounter one?

Here is another way to do it:

use strict; my (@info, $found); my $totalprice = 0; print "\n\nAuthor to search for: "; chomp (my $search = <STDIN>); print "\n\n"; while (<DATA>){ chomp $_; my ($title, $author, $genre, $price) = split ":"; if (lc $search eq lc $author) { $totalprice += $price; push @info, $_; $found++; } } if ($found) { print "\n\nNumber of matching books by $search: ", $found, "\n\n"; print pack('A40 A20 A7','Title','Genre', 'Price'),"\n"; print '-' x 39, ' ', '-' x 19, ' ', '-' x 7, "\n"; foreach (@info) { my ($title, $author, $genre, $price) = split ":"; print pack("A40 A20 A7", $title, $genre, $price), "\n"; } print "\n\tTotal price of matching book(s): $totalprice\n"; } else { print "\nNo matching books for author: $search\n\n"; } __DATA__ Taliesin:Lawhead:Celtic Novel: 8.99 History of the 20th Century:Sage:History:27.99 The Twilight of Courage:Thoene:Historical Novel:15.99 Historical Perspectives:Lawhead:History:35.00

Results:
Author to search for: lawhead Number of matching books by lawhead: 2 Title Genre Price --------------------------------------- ------------------- ------- Taliesin Celtic Novel 8.99 Historical Perspectives History 35.00 Total price of matching book(s): 43.99

--Jim


In reply to Re: Searching input file by jlongino
in thread Searching input file by psychoto

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.