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.change my ( $age, $gender, $address ) = split /,/, $rest; to my ($address) = (split /,/, $rest)[-1]; or my ($address) = (split /,/, $rest)[2];
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.
In reply to Re^5: Splitting multiple patterns
by Marshall
in thread Splitting multiple patterns
by astronogun
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |