Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^5: Splitting multiple patterns

by Marshall (Canon)
on Apr 09, 2012 at 19:07 UTC ( [id://964191]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Splitting multiple patterns
in thread Splitting multiple patterns

I'm not stevieb, but I think I can help you.
Update: mis-spelled stevieb's name previously - my goof!
To just get the address, use a "list slice". Let split do its thing and just select the parts that you want.
change my ( $age, $gender, $address ) = split /,/, $rest; to my ($address) = (split /,/, $rest)[-1]; or my ($address) = (split /,/, $rest)[2];
Perl can use a negative index! -1 in this case means "the last thing". index of 2 means 3rd thing from the left. Which one you use is dependent upon both preference and situation. In this case, the results should be identical.

As another point, if for example the address may contain comma's itself (maybe a street address in addition to the city name), you can limit the split with the 3rd parameter. (split /,/,$rest,3) allows max of 3 things in the split - that way the whole address would appear as the "last thing". BTW: I would use $city instead of $address if this is just a $city name (personal preference because $address implies something far more than just $city to me).

If this is a more complicated CSV file, with embedded commas which are quoted, using one of the CSV modules to parse this is the way to go - the CSV format is deceptively simple sounding, but the complications escalate dramatically when a comma (the field separator) can appear within one of the data fields.

Replies are listed 'Best First'.
Re^6: Splitting multiple patterns
by astronogun (Sexton) on Apr 10, 2012 at 09:35 UTC

    Thanks Marshall for the code and explanation it really helped me sorry for the late reply

    To all who contributed thanks a lot specially to sir: stevieb and Marshall :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-26 00:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found