in reply to Searching input file
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
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Searching input file
by psychoto (Novice) on Feb 28, 2002 at 06:35 UTC |