Debbie,

Debbie Christensen wrote:
> 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

There's surely other ways to do this, and many may be better, but here
is a solution for you.  It makes use of hashes, and as such may be a
bit mysterious until you get read up.  It could be cleaner, but as you
will see, your data was formatted a bit funkily, and I wasn't sure if
that was a typo or purposeful, so I accomodated the funk. :)

----- BEGIN SAMPLE CODE
#!/usr/bin/perl -w

open(STATES, "states.txt") || die "Couldn't open states.txt: $!";
while ($current_line = <STATES>) {
   # Remove trailing newline from $current_line
   chomp $current_line;
  
   # Split $current_line into two fields
   # ( split()'s pattern would've been simpler, but
   # I wanted to cover your rather wierdly spaced
   # data (see the very end of this code for
   # explanation))
   ($state, $code) = split(/ *: */, $current_line);
  
   # We use a hash to keep track of the data.  Key
   # is the number code, and the value will be a
   # concatenated list of comma-and-space separated state codes.
   if ( $seen_state{$code} ) {
      $seen_state{$code} = $seen_state{$code} . $state . ', ';
   } else {
      $seen_state{$code} = $state . ', ';
   }
}
close(STATES);

# Now cycle through the %seen_state hash, removing
# trailing comma-and-space from each value, and then
# printing the key and value.
while ( ( $key, $value ) = each %seen_state ) {
   chop $seen_state{$key}; # Get rid of the trailing space
   chop $seen_state{$key}; # Get rid of the trailing comma
   print $key, ' ', $seen_state{$key}, "\n";
}

# And that's all the work; the stuff below is for human
# readers.
=pod

Data in 'states.txt' was taken verbatim from original
posting, and as such, has some strange formatting bits:
---- Begin data ----
OH:  702
PA: 702
ND: 702
NJ :703
NY: 703
CA: 703
---- End data ----
=cut
---- END SAMPLE CODE

And that's it.  Hopefully not overwhelming, but it works,
and can serve as a basis for further questions. :)

John
-- 
               John Fox <[EMAIL PROTECTED]>
    System Administrator, InfoStructure, Ashland OR USA
  ---------------------------------------------------------
"What is loved endures.  And Babylon 5 ... Babylon 5 endures."
              --Delenn, "Rising Stars", Babylon 5

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to