Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New with Perl search... 2

Status
Not open for further replies.

LotoL

Programmer
Mar 17, 2004
20
CA
Hello,

What I need help with is to search an input file with a specific text givin by user (this is ok with me). The next step is what Im having problems with, see the user only know a certain input. Will call it Cars. What the user needs is how many car are available.

usrer input- ./available Cars
output to user- Number of Cars available = 25


example of input file..

Number of Cars available
there are 25 left.

Number of Trucks abvailable
there are 60 left.

Number of Vans abvailable
there are 60 left.

so on .....



 
So the user says 'cars' and the program should say back '25'? What code do you have so far?
 
the only code I have is to search for "Cars" Im not even sure it this will work... Just start writing perl 2 days ago.

$record = <STDIN>;
chop ($record);
open (INPUT, "inventorylist.txt") or die "Nope!! $!\n";

while ($record = INPUT) {
print $record;
if ($record =~ "Cars") {
print ("Number of Cars available = \n");
}

}
close(INPUT);
 
this works o.k. ...

Code:
print "request? : ";
chomp ($request = <>);

undef $/;
$data = <DATA>;

$data =~ m|$request[^\d]+(\d+)|i;

if ($1) {
  print "there are $1 $request available\n";
} else {
  print "we do not stock $request - sorry!\n";
}

__DATA__
Number of Cars available
there are 25 left.
Number of Trucks available
there are 60 left. 
Number of Vans available
there are 60 left.

Code:
request? : cars
there are 25 cars available
request? : vans
there are 60 vans available
request? : boats
we do not stock boats - sorry!


Kind Regards
Duncan
 
thanks duncdue, but what does this line do
$data =~ m|$request[^\d]+(\d+)|i;

i know that $data will be assign, then it does a search for, then the next part I do not know and why if ($1).

 
does it work o.k. for you?

Code:
[b]m|       = match start
$request = whatever was entered by user
[^\d]+   = one or more non-digit character
(\d+)    = one or more digit character & store as $1
|        = match end[/b]

if ($1) just means if there is a value in $1 do this else do something else...


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top