From: ccc31807 on 26 Mar 2010 11:23 On Mar 26, 7:25 am, Tad McClellan <ta...(a)seesig.invalid> wrote: > #!/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.
From: Peter J. Holzer on 27 Mar 2010 14:29 On 2010-03-26 13:48, J�rgen Exner <jurgenex(a)hotmail.com> wrote: > 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 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
From: J�rgen Exner on 27 Mar 2010 14:48 "Peter J. Holzer" <hjp-usenet2(a)hjp.at> wrote: >On 2010-03-26 13:48, J�rgen Exner <jurgenex(a)hotmail.com> wrote: >> 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 > >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
From: Jens Thoms Toerring on 27 Mar 2010 18:34 Jürgen Exner <jurgenex(a)hotmail.com> wrote: > "Peter J. Holzer" <hjp-usenet2(a)hjp.at> wrote: > >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". 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 -- \ Jens Thoms Toerring ___ jt(a)toerring.de \__________________________ http://toerring.de
From: ccc31807 on 27 Mar 2010 18:56 On Mar 27, 1:48 pm, Jürgen Exner <jurge...(a)hotmail.com> wrote: > 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
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: FAQ 9.20 How do I send mail? Next: FAQ 5.11 How can I write() into a string? |