Hi,

I have a hash which stores numerical data and I want to increase the values by variable amounts each time round a loop.

At the moment, I am fetching the value to an SV, coercing that into an IV, increasing the value, and then making a new SV from that to store back into the hash:

HV *hash;
int total;
int num;
...
total = SvIV(*hv_fetch(hash, "total", 5, 0));
total += num;
hv_store(hash, "total", 5, newSViv(total), 0);

This seems a little long-winded. Is there a way to directly increase the numerical value of the fetched SV, or even of the hash value itself?

All I can find is sv_inc(), but that only increments by 1 and I can't get it working either:

HV *hash;
SV *svtotal;
...
svtotal = *hv_fetch(hash, "total", 5, 0);
sv_inc(svtotal);
hv_store(hash, "total", 5, svtotal, 0);

This makes Perl output errors relating to "Attempt to free unreferenced scalar" back in the Perl, and program crashes with an Application Error.

Any ideas?

Steve




Reply via email to