Joseph Paish wrote:
>
> On Thursday 03 April 2003 08:53, Joseph Paish wrote:
> >
> > i have a hash that looks like this :
> >
> > abc => 123 456 789 246 346 8765
> > deh => 123 456 258 246 346 8765
> > nyx => 123 456 149 246 346 8765
> > pob => 123 456 354 246 346 8765
> > syt => 123 456 924 246 346 8765
> >
> > already sorted by the key (the first field above).
> >
> > i want to do some simple math based on the values of the third field.
> >
> > for example, replace the value of 789 in the first record above with 1578
> > (twice it's value) or print out any hashes that have a value in the third
> > field that is less than 500.
> >
> > this is easy enough to do with arrays, but i have so far been unsuccessful
> > finding how to manipulate individual "elements" in hashes.
As you say, it is easy to do with arrays, so you should use arrays:
my %last = (
abc => [ 123, 456, 789, 246, 346, 8765 ],
deh => [ 123, 456, 258, 246, 346, 8765 ],
nyx => [ 123, 456, 149, 246, 346, 8765 ],
pob => [ 123, 456, 354, 246, 346, 8765 ],
syt => [ 123, 456, 924, 246, 346, 8765 ],
);
> well, i figured out part of it shortly after posting the question. to access
> individual "elements", i did the following :
>
> foreach my $key (sort keys %last) {
> # %last is the name of the hash ... now it is sorted by key
> my @temp_array = split / /, $last{$key} ;
> print $temp_array[2] ; # prints out the 3rd field
> }
>
> now, how to do math on it? in other words, i guess i am asking how to
> convert a string '789' to a floating point number so i can do things like
> multiply it.
for my $key ( keys %last ) {
print $last{ $key }[ 2 ]; # prints out the 3rd field
$last{ $key }[ 2 ] *= 55; # multiply 3rd field by 55
}
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]