Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^4: Splitting multiple patterns

by astronogun (Sexton)
on Apr 09, 2012 at 10:30 UTC ( [id://964104]=note: print w/replies, xml ) Need Help??


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

Hi stevieb,

One last question. How about if want to get only the $address? and only print the "Address:"? What I want is to split the Name up to Gender so that it will only read the $address

Thanks

Replies are listed 'Best First'.
Re^5: Splitting multiple patterns
by Marshall (Canon) on Apr 09, 2012 at 19:07 UTC
    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.

      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://964104]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-16 09:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found