Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Problem with push() Function

by ikegami (Pope)
on Jul 22, 2009 at 23:12 UTC ( #782485=note: print w/replies, xml ) Need Help??


in reply to Problem with push() Function

To common methods.

Group and peek at previous:

my @country_states; for (@logfiles) { my $country_state = $_; # Avoid clobbering @logfiles $country_state =~ s/.txt//; $country_state =~ s/_/ /g; push @country_states, $country_state; } @country_states = sort @country_states; my $last_country; for my $country_state (@country_states) { my ($country, $state) = split(/-/, $country_state, 2); if (!defined($last_country) || $last_country ne $country) { $last_country = $country; print(uc($country), "\n"); } print($state, "\n"); }

Group into a multi-dimensional structure:

my %states_by_country; for (@logfiles) { my $country_state = $_; # Avoid clobbering @logfiles $country_state =~ s/.txt//; $country_state =~ s/_/ /g; my ($country, $state) = split(/-/, $country_state, 2); push @{ $states_by_country{$country} }, $state; } for my $country (sort keys %states_by_country) { print(uc($country), "\n"); my $states = $states_by_country{$country}; for my $state (sort @$states) { print($state, "\n"); } }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://782485]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2018-07-17 02:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (353 votes). Check out past polls.

    Notices?