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.
| [reply] [d/l] |
|
Thank you so much.
Can i store the position value in another variable like this? my $index1=($index+=1);
my $index2=($index+=2);
| [reply] |
|
"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.
| [reply] |
|
| [reply] |
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. | [reply] [d/l] [select] |
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:
-= , *= , /= | [reply] |
|
+= 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...
| [reply] |
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...)
| [reply] [d/l] [select] |
|
| [reply] [d/l] |
|
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.
| [reply] [d/l] |
Re: is $i=+3 valid?
by davido (Cardinal) on Jul 05, 2013 at 07:04 UTC
|
| [reply] |
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. | [reply] |