I took 2 looks at this problem and figured i'd need to use a hash of array
references.
now, i'm debating whether i should just hand you the code to get this job
done, or whether to realy explain how these things work.
i decided to do a bit of both, so bear with me here.
what we want is a list of states that one thing in common: their number
(i'm not american, so i'm just guessing it's an area code or something, but
it doenst really matter)
so they all have a KEY. if you say key, you say hash... so we'll use a hash
to store these lists in.
now we need to bear 2 things in mind:
1 if we store things in hashes, they have to be scalars if anything... so we
have to use array references
2 we need to check whether a key already exists or not. if so, we add the
entry to the list represented by the key, if it doesnt exist yet, we make it
and go on our merry way.
let's start with the code:
### open the file ###
open I, "state.txt.txt" or die $!;
### declare a hash ###
my %codes;
### start reading the file ###
while (<I>) {
chomp; #remove newline
s/\s+//g; #remove white space
my @entry = split ':'; #split on :
### check if there is already a key by the name of whatever was in the 2nd
part of the string
### ie, this would check if '$codes{702}' already existed or not
unless ( defined $codes{$entry[1]} ) {
### if it doesnt exist yet, we make the entry, along with an array
reference holding the state
### the state is held in the first element of @entry: which is
$entry[0]
$codes{$entry[1]} = [ $entry[0] ];
} else {
### if the key already exists, we add the state to the list already
represented by the key using 'push'
### we DO have to dereference the array ref to be able to do that,
that's why there's a @{ .. } around it
push @{$codes{$entry[1]}}, $entry[0];
}
}
### now let's print it out
### sort the hash keys first, meaning we print 401 before 804 etc
for (sort keys %codes) {
### $_ has the key, say '701' then we print a ' = ' after that, then
join all the elements in our list represented by
### that key with a comma, then we add a newline. just like in the
'else' block above, we have to dereference the array
### again, meaning we put the @{ .. } around it.
print $_, " = ", join ',',@{$codes{$_}}, "\n";
}
This is quite a heavy bit of code for a newcomer to perl, but there are some
tutorials available on it if you'd like.
i wrote some on http://japh.nu
I advice you read the data types tutorial, then the loops/blocks tut,
followed by the reference tutorial
with a little luck, all the above code will then make perfect sence to you.
good luck,
Jos Boumans
> 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
>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]