Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1035494]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (13)
As of 2018-06-20 16:27 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.