Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Floats with trailing zeros as a hash key

by robartes (Priest)
on May 09, 2003 at 15:05 UTC ( [id://256897]=note: print w/replies, xml ) Need Help??


in reply to Floats with trailing zeros as a hash key

Hmm,

storing these with the prices as keys, as opposed to the more natural way of doing it the other way around, seems a bit funny to me. Do you perhaps want fast lookups of the prices?

That said, there is no real need to store the two keys. Perl is smart enough to use the key in correct context. If you want to make sure the number gets printed with the trailing zero, use printf:

$ perl -e 'printf ("%0.2f", 45.2)' 45.20

This way, there's no need to store two versions of the key. Let Perl convert to the correct context if needed, and use printf if you want to pretty print.

CU
Robartes-

Replies are listed 'Best First'.
Re: Re: Floats with trailing zeros as a hash key
by hardburn (Abbot) on May 09, 2003 at 15:40 UTC

    Do you perhaps want fast lookups of the prices?

    Yes, exactly. This actually a rewrite of another script. The orginal programmer used a long series of if/elsif statements. This is how I cleaned it up.

    This way, there's no need to store two versions of the key.

    The real problem comes like this:

    use CGI qw(:standard); my $input_item = param('item') || 0; my $subtotal; my $item_name; if(exists $ITEMS{$input_item}) { $subtotal = $input_item; $item_name = $ITEMS{$input_item}; }

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      Hmmm, I think robartes is on to something, the main problem is that you are using a price as a key. It only happens to work because all the prices are different, which is hardly a logical necessity. If at all possible, change the web page so that you use some artificial key, from which you can look up the name and the price.
      You could try canonicising (sp? Heck, is that even a word?) the prices before storing them, e.g. like so:
      my $key = sprintf("%0.4f", $price); $items{$key}=$item;
      You basically force the numbers in a certain format (rounded to 4 decimals and zero padded in this case). That way, you are guaranteed that 45.20 and 45.2 will end up as 45.2000 and thus in the same hash entry.

      Now remind me again why you want to put the prices in the keys? What happens if you have two items with equal prices?

      CU
      Robartes-

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://256897]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-20 06:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found