sara_perl has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: I ve an urgent issue..
by wfsp (Abbot) on Mar 20, 2008 at 15:14 UTC
    I second halfcountplus's point about the code tags, especially as it is urgent.

    You were heading in the right direction with your first for loop - but it got away from you (don't you just hate it when that happens?). Use strict and warnings _and_ don't comment them out. :-)

    Read up on Perl data structures, what you needed was a hash of arrays. e.g. Data Types and Variables

    Good luck!

    #!/usr/bin/perl use warnings; use strict; my @country = ( "Australia", "south","Austria","America","India","Zimbabwe" ); # build a table - a hash of arrays (HoA) my %table; foreach my $country (@country) { my $init = uc substr($country,0,1); push @{$table{$init}}, $country; } # output the table for my $letter (sort keys %table){ print qq{$letter = }; for my $country (sort @{$table{$letter}}){ print qq{$country }; } print qq{\n}; }
    produces
    A = America Australia Austria I = India S = south Z = Zimbabwe
    updated: added link
Re: I ve an urgent issue..
by apl (Monsignor) on Mar 20, 2008 at 15:15 UTC
    You have far too much code for such a trivial task... You should ask your teacher for a tutor.

    I will give you a clue, though. Think hash.

    Longer clue: Implement a hash where the value is the concatenation of all names starting with the same first letter, and where the key is the first letter .

Re: I ve an urgent issue..
by halfcountplus (Hermit) on Mar 20, 2008 at 14:49 UTC
    As it says at the bottom of the "ask your question" page, if your post doesn't look the way anyone would want it to look:

  • See Writeup Formatting Tips and other pages linked from there for more info.

    Your biggest, most important problem now is to "update" your question and make sure you have <code> BEFORE the code and </code> AFTER the code. Otherwise the chances of someone reading and answering your question are lower.
Re: I ve an urgent issue..
by NetWallah (Canon) on Mar 20, 2008 at 18:38 UTC
    Here you go..
    my @c=("Australia", "south","Austria","America","India","Zimbabwe"); my %h; push @{ $h{uc substr($_,0,1)} }, $_ for @c; for my $l(sort keys %h){ print "$l\n=\n"; print "$_\n" for sort @{$h{$l}}; print "\n" }

         "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

Re: I ve an urgent issue..
by bart (Canon) on Mar 20, 2008 at 21:59 UTC
    my @country = ("Australia", "south","Austria","America","India","Zimba +bwe"); my %list; foreach (@country) { /^(.)/ and push @{$list{uc $1}}, $_; } foreach (sort keys %list) { print "$_\n=\n"; print "$_\n" for sort @{$list{$_}}; print "\n"; }
    That's all I'm saying.