Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Number notation

by Michalis (Pilgrim)
on Jun 20, 2001 at 22:08 UTC ( [id://90115]=perlquestion: print w/replies, xml ) Need Help??

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

Ok, my english is not good, so i'll try with an example:
(very much of the code is snipped out, as useless and to save space)

$string1='2000-02'; $string2='2000-2'; ($var1,$var2) = split(/-/,$string); ($var3,$var4) = split(/-/,$string); my %array = (1, 30, 2, 125);

Why is $array{$var2} undef and $array{$var4} == 125 while 2 == 02?
How can I come over it?

Thanks in advance for any answers

Replies are listed 'Best First'.
Re: Number notation
by Sifmole (Chaplain) on Jun 20, 2001 at 22:15 UTC
    Well..
    my %array = (1, 30, 2, 125);
    sets up a hash with 2 keys, 1 and 2. The values of which are 30 and 125 respectively. If you wrote it as such it might be more visible..
    my %array = ( 1 => 30, 2 => 125);
    You are querying the hash with keys of "02" and "2" from your splits. There is no key "02", since this is different from simply "2", thus "undef". There is a key "2" and you get the value of it "125".

    Your statement 2 == 02 is true, however the keys of a hash are treated as strings and "2" eq "02" is false.

      <Quote>
      Your statement 2 == 02 is true, however the keys of a hash are treated as strings and "2" eq "02" is false.
      </Quote>

      Actually that was the only thing I 've read so far, and I wasn't aware of, and that's the answer to the problem. Thanks.
      Well, learning while aging is a good thing anyway:)

Re: Number notation
by srawls (Friar) on Jun 20, 2001 at 22:15 UTC
    because 2 == 02 is in numeric context. $array{$var2} is in string context. Just add int to the code like such:
    print $array{int($var2)}

    The 15 year old, freshman programmer,
    Stephen Rawls

      Thanks, the usage of int() is a solution, and probably the one I'll follow (although, the hash is so small that i'ld be probably better to just add the extra keys :-)

Re: Number notation
by VSarkiss (Monsignor) on Jun 21, 2001 at 00:01 UTC
    There are several good answer aboves. I just wanted to point out that you must have made a mistake transcribing the code. You're assigning values to $string1 and $string2, but using split on a different variable $string (no number). The code as you have it here sets both $var2 and $var4 (and the others) to undef.
      Thanks. That was a typo, I meant $string1 and $string2 respectively
Re: Number notation
by cLive ;-) (Prior) on Jun 20, 2001 at 22:18 UTC
    You have a hash with two name/value pairs.
    $var1 = "2000"; $var2 = "02"; $var3 = "2000"; $var4 = "2"; $var2 does not get interpolated - ie, "02" is not the same as "2" in t +he context you used. To force numerical context: $array{$var2} = $array{"02"} = undefined $array{$var2+0} = $array{"2"} = 125
    1 and 2 are the only KEYS in the hash, so others are undefined. And I would rename %array to %hash to avoid confusion with this.

    I suggest you read perldata document to familiarise yourself with Perl's data structures.

    cLive ;-)

    Update: - oops, misread (thanks Sifmole) - have amended above to explain.

      cLive -- The exact confusion here is that "02" does NOT become "2" in $array{$var2}. If it did, then his result from $array{$var2} would be "125" not "undef".

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-24 23:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found