> I have some data that needs to be split up alphabetically into lists 
> that start with each letter of the alphabet.  Sample data is like the 
> following with real URL data in place of URL1 and URL2.  Only 
> the first 
> field is important for alphabetizing.  If I do an array with 
> each letter 
> (and it can be assumed that it will always start with a 
> capital letter), 
> how can I do something like the following?
> 
> foreach $letter (@alphabet) {
>    if firstchar of $data = $letter {
>      push @{"$letter"}, $data
>    }
> }
> 
> sample data:
> 
> Psychology|URL1|URL2|
> Notes and Queries|URL1|URL2|
> ACM|URL1|URL2|
> Biology|URL1|URL2|

use strict;
use warnings;
use Data::Dumper;

my %data;
while(<DATA>)
{
        chomp;
        my ($str, @rest) = split /\|/;
        # use 'lc substr...' if you want case
        # insensitivity
        my $letter = substr $str, 0, 1;
        $data{$letter}{$str} = [EMAIL PROTECTED];
}
print Dumper(\%data);

__DATA__
Psychology|URL1|URL2|
Notes and Queries|URL1|URL2|
ACM|URL1|URL2|
Biology|URL1|URL2|

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to