There's more than one way to do things 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");
}
}

Create A New User
Node Status?
node history
Node Type: note [id://782485]
help
Chatterbox?
 [marto]: Good morning all

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2017-12-13 08:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What programming language do you hate the most?

Results (350 votes). Check out past polls.

Notices?