Daniel Rychlik wrote:
> How does one update a hash element in PERL?
>
> I am using anonymous hashes to process a file, and each element in the
> anonymous has its own value...
>
> If the file has success on process, I delete the hash element from the
> anonymous hash...
>
> If it the file fails to process, I need to increment the attempts to
> the next higher number...
>
> I tried this,
>
> If ($HASH{KEY}->{attempts} != '5') {
Do you really want "!="? Wouldn't "<" be safer just in case something
odd happened to give it a larger value?
>
> $attempted = $HASH{KEY}->{attempts} + '1';
>
> $HASH{KEY}->{attempts} => $attempted; #Update the hash key
Hmm... Remember that "=>" is equivelent to a comma, so this is wrong.
>
> } else {
>
> Move the file to the error folder...
>
> }
>
> When I run it, I get "Useless use of hash element in void context.
> Useless use of private variable in void context...
>
> How could this better be written?
Why the convolution in updating the value? Just do this:
$HASH{KEY}->{attempts}++; #Update the hash key
If you really want the $attempted variable for another purpose, you
can do it that way, just use the = instead of =>.
$HASH{KEY}->{attempts} = $attempted; #Update the hash key
--
Bowie
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs