Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Interpreting values alternately as $trings and Integers = confused response by Perl

by jerrygarciuh (Curate)
on Apr 14, 2002 at 19:54 UTC ( [id://158987]=perlquestion: print w/replies, xml ) Need Help??

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

Sober Monx,
I have run into this now 3 times and realized I need to understand some underlying mystery. The following works as I expect when $ary[0] = 12.
if ($ary[0] > 12) { $ary[0] -= 12; $ary[0] = '0'.$ary[0]; $scalar = 'foo'; } elsif ($ary[0] eq '12') { $scalar = 'foo'; } else { $scalar = 'bar'; }

However, I origianlly wrote the elsif as
} elsif ($ary[0] = '12') {
and found that $ary[0] was being assigned a value of 12.
Will someone please advise me as to why this occurs?
TIA
jg
_____________________________________________________
Think a race on a horse on a ball with a fish! TG

Replies are listed 'Best First'.
Re: Interpreting values alternately as $trings and Integers = confused response by Perl
by ariels (Curate) on Apr 14, 2002 at 19:56 UTC
    If you write "$ary[0] = '12'", you assign the value "'12'" to $ary[0]. Perhaps you were thinking of "$ary[0] == '12'"?
      Note that perl has two different kinds of comparison operators, one for strings (eq) and one for numerical values (==)

      Minor point, but you should probably use $arr[0] == 12 to get a true numerical comparison. i.e. Drop the quotes.

      elbieelbieelbie

        I agree that the quotes should probably be removed, but for reasons of clarity -- that is, people are likely to glance at the code and accidentally interpret the == as an eq due to seeing quotes. == does "true numerical comparison" whether or not there are quotes there; the string gets converted to a number for the ==, and it DWYM.

Re: Interpreting values alternately as $trings and Integers = confused response by Perl
by Juerd (Abbot) on Apr 14, 2002 at 20:07 UTC

    } elsif ($ary[0] = '12') { and found that $ary[0] was being assigned a value of 12.

    Relational operators String Number Equal eq == Not equal ne != Greater gt > Greater or equal ge >= Less lt < Less or equal le <= Compare cmp <=>
    As you can see, = is not a relational operator that test equality. What is it then? It's the assignment operator.

    You assigned 12 to $ary[0], and an assignment returns the assigned value, so the entire $ary[0] = '12' expression returns 12, which evaluates to true in boolean context. You probably meant == or eq, depending on how you want to see things. If you use ==, drop the quotes so that Perl doesn't have to convert the string 12 to the number 12.

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

jerrygarciuh flogs-self with the trout of rebuke.
by jerrygarciuh (Curate) on Apr 14, 2002 at 21:40 UTC
    jerrygarciuh flogs his braindead idiot self with the trout of rebuke.
    Sorry for inane question, BTW I don't know how I missed in in my perl -c checks earlier but I just did get a warning that I probably intended to use == when I ran a command line check and a second instance of this mistake cropped up. Slap, slap, slap.
    Blushing,
    jg
    _____________________________________________________
    Think a race on a horse on a ball with a fish! TG
Re: Interpreting values alternately as $trings and Integers = confused response by Perl
by CukiMnstr (Deacon) on Apr 14, 2002 at 20:37 UTC
    he. I guess you really meant the "Sober Monx" part ;-)
Re: Interpreting values alternately as $trings and Integers = confused response by Perl
by Anonymous Monk on Apr 15, 2002 at 10:55 UTC
    It looks like you used "=", which is assignment, hence Perl is assigning the value 12 to your array element.

    On the other hand, your if statement was trying to check if $ary[0] was equal to 12, so you should have used "==" (with no quotes, of course!) as follows :

    } elsif ($ary[0] == ...

    Hope this helps.

    Emanuela.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-19 06:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found