- Jul 20, 2001
- 72,100
- 32,406
- 136
I have a text file like so:
Bob's House, Smallville, Oregon, USA
Bigtown, New York, USA
Argentina
Ontario, Canada
Nice Beach, Green Lake, Big Province, Poland
Each line holds a separate geographical location, ordered from most specific to country. Commas are used to separate values. All entries have at least the country.
What I want is:
USA, Oregon, Smallville, Bob's House
USA, New York, Bigtown
Argentina
Canada, Ontario
Poland, Big Province, Green Lake, Nice Beach
It's been years since I've done any programming so I'm not sure how to approach this. I can load each line into an array and use the pop function to pull the last value on a line and start building a reversed array for each line in the correct order but I'm trying to figure out how to deal with the fact that there aren't an equal number of values on each line. Perl's the hammer I'm most familiar with. Any ideas?
Bob's House, Smallville, Oregon, USA
Bigtown, New York, USA
Argentina
Ontario, Canada
Nice Beach, Green Lake, Big Province, Poland
Each line holds a separate geographical location, ordered from most specific to country. Commas are used to separate values. All entries have at least the country.
What I want is:
USA, Oregon, Smallville, Bob's House
USA, New York, Bigtown
Argentina
Canada, Ontario
Poland, Big Province, Green Lake, Nice Beach
It's been years since I've done any programming so I'm not sure how to approach this. I can load each line into an array and use the pop function to pull the last value on a line and start building a reversed array for each line in the correct order but I'm trying to figure out how to deal with the fact that there aren't an equal number of values on each line. Perl's the hammer I'm most familiar with. Any ideas?
Last edited: