Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

is $i=+3 valid?

by rocketperl (Sexton)
on Jul 05, 2013 at 07:02 UTC ( [id://1042618]=perlquestion: print w/replies, xml ) Need Help??

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

$i++ increments its value by 1. but i want my value to increment by 3. so is $i=+3 valid? thanks

Replies are listed 'Best First'.
Re: is $i=+3 valid?
by kcott (Archbishop) on Jul 05, 2013 at 07:10 UTC

    G'day rocketperl,

    No. You're almost there, though: try $i += 3

    All of those types of operators are described in perlop - Assignment Operators. Actually, you'd do well to, at least, scan through all of perlop to learn what operators Perl provides.

    -- Ken

      Thank you so much. Can i store the position value in another variable like this? my $index1=($index+=1); my $index2=($index+=2);
        "Thank you so much."

        You're welcome.

        "Can i store the position value in another variable like this? my $index1=($index+=1); my $index2=($index+=2);"

        You need to try this yourself! If you make mistakes, that's great: you'll learn something and, hopefully, won't make those mistakes again. I've provided you with specific documentation for those operators as well as more general documentation for all operators: please read it. hdb (in Re: is $i=+3 valid?) has recommended you experiment and has even provided sample code: heed his advice.

        If, after trying this for yourself, you get stuck or don't understand some aspect of what's going on, then come back and ask. However, before asking, ensure you have read "How do I post a question effectively?" and that your post follows its guidelines.

        Furthermore, I recommend you go over your previous posts (that's seven threads) and read the advice given. There's a distinct trend of: help being asked for; help being given; help being ignored; same help being asked for again; frustrated monks pointing you back to help that was given only a day or two before.

        We're happy to help if you show some effort yourself.

        -- Ken

        What do you think this code does? What happened when you tested it? What do you actually want to do?

Re: is $i=+3 valid?
by dsheroh (Monsignor) on Jul 05, 2013 at 07:28 UTC
    Yes, $i=+3 is valid, but it doesn't do what you intend. It sets the value of $i to positive three (+3).

    As earlier answers suggested, you would use $i += 3 to add 3 to the value of $i.

Re: is $i=+3 valid?
by Rahul6990 (Beadle) on Jul 05, 2013 at 07:09 UTC
    The correct way to use it is :

    $i+=3;

    += is called the short hand operator, similar to this you can use:

    -= , *= , /=
      += is called the short hand operator...

      I've never before seen or heard it so called. This is not important, just idle curiosity, but can you cite an example of this usage?

      Update: Actually, Googling just now found a number of examples of this usage. Never mind...

Re: is $i=+3 valid?
by hdb (Monsignor) on Jul 05, 2013 at 07:27 UTC

    You got your answer already to change =+ to +=. However, why do not just experiment a little bit. It is really easy to write:

    use strict; use warnings; my $i=0; $i=+3; print "$i\n"; # no errors, so far, prints 3, so we should be fine...? $i=+3; # better try again print "$i\n"; # still prints 3, so something must be wrong! $i=$i+3; # check whether Perl works correctly... print "$i\n"; # prints 6, what a relief

    Experiments are fun! (Unless you are a theoretical physicist...)

      However, why do not just experiment a little bit.

      Or you make it a rhyme. Need to re-name $i to $e to make it a rhyme:

      ++$e for 1 .. 3;

      Cheers, Sören

      (hooked on the Perl Programming language)

        How to increment by 3, is wondering he (or she). Plus equal or equal plus, what a fuzz! Just try it out and see or stay in the dark will he.
Re: is $i=+3 valid?
by davido (Cardinal) on Jul 05, 2013 at 07:04 UTC

    +=


    Dave

Re: is $i=+3 valid?
by sahil2588 (Novice) on Jul 05, 2013 at 07:42 UTC
    I think you are trying to increment the value of $i here. For that you should use: $i+=3 This will add 3 to your current $i value. $i=+3 is just like assigning "+3" i.e. positive 3 value to $i.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2024-04-19 16:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found