From: Ninja Li on
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

From: Peter Makholm on
Ninja Li <nickli2000(a)gmail.com> writes:

> 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
From: RedGrittyBrick on
On 26/03/2010 07:46, Ninja Li wrote:
> 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.

--
RGB
From: Tad McClellan on
Ninja Li <nickli2000(a)gmail.com> wrote:

> I have a file with two fields, country and city and "|" delimiter.


You have a " | " *separator*, not a delimiter.


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

-----------------------
#!/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
-----------------------


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
From: J�rgen Exner on
Ninja Li <nickli2000(a)gmail.com> wrote:
> 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