Using a hash would have been my first choice, however the requester didn't
state they needed to save the data into a structure to access later, only
that the states with like codes needed to be presented on a single line
of output.

Ask 100 programmers and you'll usually find 100 ways of solving a given
problem.  The code presented solved the problem presented and it is often
best not to involve extra data structures if their use isn't required.

One other point, your code presents states with unique codes as:

CODE STATE,

Just thought you might want to know.

Will

-- Original Message --

>Maybe using a hash could make things much simpler:
>
>open (STATES, "<state.txt") || die ("can't open state.txt");
>while (<STATES>)
>{
>    if (/^(\w+)\:\s+(\d+)$/)
>    {
>       $states{$2} .= "$1, ";
>    }
>}
>close (STATES);
>
>foreach $code (sort keys (%states))
>{
>    chop $states{$code};
>    chop $states{$code};
>    print "$code $states{$code}\n";
>}
>
><br><br><br>Best Regards,
>
>Katherine Qiang
>http://home.cwru.edu/~qxq2
>http://kittyqiang.tripod.com
>
>
>
>>From: "Will Crain" <[EMAIL PROTECTED]>
>>To: "Debbie Christensen" <[EMAIL PROTECTED]>,
>>[EMAIL PROTECTED]
>>Subject: RE: reading a text file
>>Date: Wed, 25 Jul 2001 12:24:58 -0500
>>
>>Debbie, your problem seemed easy enough.  See if this works for you:
>>
>>my $state;
>>my $code;
>>my $currCode = 0;
>>my $outLine = "";
>>
>>open(STATES, "<state.txt") || die("can't open state:");
>>while($line = <STATES>) {
>>
>>      $line =~ /^(\w+): (\d+)/;
>>      if($code == $currCode) {
>>              $outLine .= ", " . $1;
>>      }
>>      else {
>>              unless($outLine eq "") { print("$outLine \n"); }
>>              $outLine = $2 . " " . $1;
>>      }
>>}
>>
>>print("$outLine \n");
>>close STATES
>>
>>Caveat:  this script assumes all input follows as per your example.
>>
>>
>>Will
>>
>>
>>-- Original Message --
>>
>> >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]
>> >
>> >
>>
>>
>>
>>
>>Visit iWon.com - the Internet's largest guaranteed cash giveaway! Click
>>here now for your "Thank You" gift:
>>http://www.iwon.com/giftcenter/0,2612,,00.html?t_id=20157
>>
>>
>>
>>
>>--
>>To unsubscribe, e-mail: [EMAIL PROTECTED]
>>For additional commands, e-mail: [EMAIL PROTECTED]
>>
>
>
>_________________________________________________________________
>Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
>
>
>--
>To unsubscribe, e-mail: [EMAIL PROTECTED]
>For additional commands, e-mail: [EMAIL PROTECTED]
>
>




Visit iWon.com - the Internet's largest guaranteed cash giveaway! Click
here now for your "Thank You" gift:
http://www.iwon.com/giftcenter/0,2612,,00.html?t_id=20157




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

Reply via email to