File formatting

N

Ninja Li

Hi,

I have a file with two fields, country and city and "|" delimiter.
Here are the sample formats:

USA | Boston
USA | Chicago
USA | Seattle
Ireland | Dublin
Britain | London
Britain | Liverpool

I would like to have the output like the following:
USA | Boston, Chicago, Seattle
Ireland | Dublin
Britain | London, Liverpool

I tried to open the file, use temp variables to store and compare
the countries and it looks very cumbersome. Is there an easier way to
tackle this?

Thanks in advance.

Nick
 
P

Peter Makholm

Ninja Li said:
I tried to open the file, use temp variables to store and compare
the countries and it looks very cumbersome. Is there an easier way to
tackle this?

Basically I would open the file, and use some variables to store the
content in some useful data structure. How cumbersome it gets depends
on the specific way that you store the date.

But if you post some code we might be able to improve on it.

//Makholm
 
R

RedGrittyBrick

Hi,

I have a file with two fields, country and city and "|" delimiter.
Here are the sample formats:

USA | Boston
USA | Chicago
USA | Seattle
Ireland | Dublin
Britain | London
Britain | Liverpool

I would like to have the output like the following:
USA | Boston, Chicago, Seattle
Ireland | Dublin
Britain | London, Liverpool

I tried to open the file, use temp variables to store and compare
the countries and it looks very cumbersome. Is there an easier way to
tackle this?

I'd use split and push onto an arrayref stored in a hash. Or just append
to a hash value with an initial comma if the hash key exists.
 
J

Jürgen Exner

Ninja Li said:
I have a file with two fields, country and city and "|" delimiter.
Here are the sample formats:

USA | Boston
USA | Chicago
USA | Seattle
Ireland | Dublin
Britain | London
Britain | Liverpool

I would like to have the output like the following:
USA | Boston, Chicago, Seattle
Ireland | Dublin
Britain | London, Liverpool

I tried to open the file, use temp variables to store and compare
the countries and it looks very cumbersome. Is there an easier way to
tackle this?

As the cities are obviously grouped there is no need to construct a
complex data structure. Instead just keep one $current_country and
@cities in which you just push() all cities for as long as the ocuntry
doesn't change. And while readind the file line by line whenever the
line contains a new country just do a
print "$current_country | @cities\n";
and reset $current_country to the new country and initialize @cities
with the first city of that country.

jue
 
C

ccc31807

#!/usr/bin/perl
use warnings;
use strict;

my %cities;
while ( <DATA> ) {
    chomp;
    my($country, $city) = split / \| /;
    push @{ $cities{$country} }, $city;

}

foreach my $country ( sort keys %cities ) {
    print "$country | ", join(', ', @{ $cities{$country} }), "\n";

}

__DATA__
USA | Boston
USA | Chicago
USA | Seattle
Ireland | Dublin
Britain | London
Britain | Liverpool
-----------------------

I think this is the ideal solution. You might want to check to see
that city names with spaces (like 'New York' look like in the array.
The only thing I would add is that your data structure (in memory)
looks like this:
%cities = {
USA => [Boston Chicago Seattle],
Ireland => [Dublin],
Britain => [London Liverpool],
}

CC.
 
P

Peter J. Holzer

As the cities are obviously grouped

You should always be wary about "obvious" patterns if all you have is a
six line example. It is entirely possible that the grouping is only by
chance and that the next line is "USA | Washington".

Never silently assume anything. If you spot a pattern, ask the customer
whether you can rely on this pattern, and if so, write it into the spec.

hp
 
J

Jürgen Exner

Peter J. Holzer said:
You should always be wary about "obvious" patterns if all you have is a
six line example. It is entirely possible that the grouping is only by
chance and that the next line is "USA | Washington".

Absolutely. That "obviously" was very much tounge in cheek.
Unfortunately I'm not aware of a well-known emoticon for
"tounge-in-cheek".
Never silently assume anything. If you spot a pattern, ask the customer
whether you can rely on this pattern, and if so, write it into the spec.

I was hoping my use of "obvious" made that, well, obvious.

jue
 
J

Jens Thoms Toerring

Absolutely. That "obviously" was very much tounge in cheek.
Unfortunately I'm not aware of a well-known emoticon for
"tounge-in-cheek".

Well, one could try use =>) or (<= - depending on which cheek
the tongue is in and from which direction you're looking at
it;-)
Regards, Jens
 
C

ccc31807

Absolutely. That "obviously" was very much tounge in cheek.
Unfortunately I'm not aware of a well-known emoticon for
"tounge-in-cheek".

I've always assumed that :p (or :p) represented the 'tongue in cheek'
mode.

CC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top