taintme has asked for the wisdom of the Perl Monks concerning the following question:

I have a script that reads values and does some stats.
I have 2 values that I read that are different BigInts and I do the same operations on each pair.
The problem is I seem to not be able to assign a new value to an existing BigInt and I believe my performance is suffering because of it. I have received many emails offering to sell me pills to enhance performance, but I thought I would ask here first.
#before loop $cntdiff = new Math::BigInt '0'; $predefbi = new Math::BigInt '0'; # inside read loop # for example $firststr = "ffffccbbaa0"; $laststr = "ffffccbbaa9"; # Make sure it is clear that its hex... $firststr = "0x".$firststr; $laststr = "0x".$laststr; $strtcnt = new Math::BigInt $firststr; $endcnt = new Math::BigInt $laststr; $cntdiff = $endcnt - $strtcnt; # the above works OK but if I try something like $predefbi = $firststr; # it doesn't seem to work
Am I SOL on this?

TIA

Replies are listed 'Best First'.
Re: Changing the value of a BigInt?
by ysth (Canon) on May 13, 2004 at 03:49 UTC
    Leaving Math::BigInt out of it, you can't have a scalar have a value like "0xdeadbeef" and have perl treat it as the number 0xdeadbeef; the "0x" header is noticed by the perl compiler when compiling a constant, and by things like Math::BigInt::new that explicitly do so, but when perl numifies a string variable, it only looks for decimal numbers. So "0xfoo" will only have the "0" part recognized as a number.

    Is there a reason you can't say $predefbi = new Math::BigInt $firststr;?

      Is there a reason you can't say $predefbi = new Math::BigInt $firststr; ?

      Or even...

      $predefbi = $strtcnt;
Re: Changing the value of a BigInt?
by bart (Canon) on May 13, 2004 at 10:04 UTC
Re: Changing the value of a BigInt?
by thor (Priest) on May 13, 2004 at 03:46 UTC
    When I see "doesn't seem to work", I ask "what is it doing, and how does that differ from your expectations?".

    Without that having been answered, I see that you're assigning the string "0xffffccbbaa0" to something that was a Math::BigInt object...

    thor

Re: Changing the value of a BigInt?
by taintme (Acolyte) on May 13, 2004 at 05:20 UTC
    Thanks for the replies so far.

    The thing I was trying to avoid was doing "new" each time I want to jam a different hex value into $strtcnt and $endcnt.
    Basically these three vars live the whole time the program is running, so I was hoping to have a way to say:
    $strtcnt = next_hex_val;
    without doing "new" but maybe new isn't what is causing slowness.
      Then you want to tie it; something like:
      { package Tie::AutoBigInt; use Tie::Scalar; use base "Tie::StdScalar"; sub STORE { $_[0]->SUPER::STORE(new BigInt $_[1]) } } tie $strtcnt, "Tie::AutoBigInt";