Here is one way(I changed file to use __DATA__). I sed a hash and
then sorted on the data within the hash on the fly.
HTH.
Wags ;)
============================================================================
======================
#!perl -w
my %States = ();
while (<DATA>) {
chomp;
my @works = split(/\s*:\s*/,$_);
$States{uc($works[0])} = $works[1];
}
my $MyCheck = '';
my $MyLine = '';
foreach my $MyData (sort {$a->[1]<=>$b->[1] || $a->[0]cmp$b->[0]} map {
[$_,$States{$_}] } keys %States ) {
if ( $MyData->[1] ne $MyCheck ) {
if ( $MyCheck gt '' ) {
$MyLine =~ s/,\s+$//g;
print "$MyLine\n" ;
}
$MyLine = sprintf "%-4s", $MyData->[1];
}
$MyLine .= sprintf "$MyData->[0], ";
$MyCheck = $MyData->[1];
}
if ( $MyLine gt '' ) {
$MyLine =~ s/,\s+$//g;
print "$MyLine\n" ;
}
__DATA__
OH: 702
PA: 702
ND: 702
NJ :703
NY: 703
Ca: 703
^---- End of data
Wags ;)
-----Original Message-----
From: Debbie Christensen [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, July 24, 2001 12:03
To: [EMAIL PROTECTED]
Subject: reading a text file
I am brand new to perl; I am only on chapt 4 of the learning perl book. My
boss has already given me a project to do that I am really struggling with.
I know you are all really busy, but I would really appreciate any help you
can give.
I have a text file that looks something like this
OH: 702
PA: 702
ND: 702
NJ :703
NY: 703
Ca: 703
#my simple program
open(STATES,"state.txt")||
die "can't open state:";
while ($line = <STATES>)
{
print $line;
}
close STATES;
I am able to open the file and read it with no problem. Where I get lost is
My boss wants the data to come out like
702 OH, PA, ND
703 NJ, NY, CA
I have looked up Faq questions and have looked through my books. I have
even tried formating it with no luck. Any help you can give would be great.
Thanks,
Debbie Christensen
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]