Tim McGeary wrote:
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?

That is a Frequently Asked Question.

perldoc -q "How can I use a variable as a variable name"


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|

It is probably better to use a hash:

my %data;
while ( <DATA> ) {
    chomp;
    my ( $letter ) = /([a-zA-Z])/;
    push @{ $data{ $letter } }, $_;
    }



John
--
use Perl;
program
fulfillment

--
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