Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Do you know where your variables are?
 
PerlMonks  

Re^3: Parse file, split

by Jim (Curate)
on May 28, 2013 at 01:07 UTC ( #1035494=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Parse file, split
in thread Parse file, split

Here's a demonstration of how using regular expression pattern matching instead of string split might be more correct, robust and extensible.

#!perl use strict; use warnings; my $valid_vehicle_description_pattern = qr{ ((?:19|20)\d\d) # $1 is Year \s+ ( # $2 is Make British\s+Leyland | Chev(?:y|rolet) | Dodge | Ford | (?:General\s+Motors|GMC?) | Hyundai | Kia | Nissan | Toyota ) \s+ (\S.*) # $3 is Model }ix; while (my $vehicle = <DATA>) { chomp $vehicle; if ($vehicle =~ $valid_vehicle_description_pattern) { my ($year, $make, $model) = ($1, $2, $3); print "Year: $year\tMake: $make\tModel: $model\n"; } else { warn "Invalid vehicle description: $vehicle\n"; } } __DATA__ 1970 British Leyland Triumph Spitfire 2011 CHEVROLET CAMARO 2011 Chevy Camaro 2011 Dodge Ram Crew Cab Short Bed 2011 Ford F150 Platinum 2011 Ford Flex 2011 Ford Transit 2011 GMC Cargo Van Extended 2011 Hyundai Genesis Coupe 2011 Kia Sol 2011 Nissan Cube 2011 Toyota Prius 2015 Apple iCar


Comment on Re^3: Parse file, split
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1035494]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2014-04-21 08:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (492 votes), past polls