Sum of two interger variables? 
Author Message
 Sum of two interger variables?

Is it possible to have a interger variable, $a for example, add
another interger variable to it, $b, and make the sum the new value of
$a?


Mon, 17 May 2004 11:12:24 GMT  
 Sum of two interger variables?

Quote:
> Is it possible to have a interger variable, $a for example, add
> another interger variable to it, $b, and make the sum the new value of
> $a?

Perl would be {*filter*}if it couldn't do that:

     $x = 5;
     $y = 3;

     $x = $x + $y;
     print "$x\n";

Or more compactly:

     $x += $y;

By the way, the reason I used $x and $y as variable names
is that $a and $b are special variables used by the sort
function. To find out more read:

     perldoc -f sort

Cheers,
Bernard



Mon, 17 May 2004 11:16:11 GMT  
 Sum of two interger variables?

Quote:

> Is it possible to have a interger variable, $a for example, add
> another interger variable to it, $b, and make the sum the new value of
> $a?

you mean like

    $a = $a + $b;

?

--

CGI Meta FAQ - http://www.perl.org/CGI_MetaFAQ.html
Troubleshooting CGI scripts - http://www.perl.org/troubleshooting_CGI.html



Mon, 17 May 2004 12:43:08 GMT  
 Sum of two interger variables?

Quote:

> Is it possible to have a interger variable, $a for example, add
> another interger variable to it, $b, and make the sum the new value
> of $a?

#!/usr/local/bin/perl -w
use strict;
my $a = 1;
my $b = 2;
$a += $b;
print "$a\n";

You should be careful about using $a and $b as variables since the
Perl sort operator uses them.  Using $a and $b for non-sort things can
cause problems if you are not very careful.  It's safer just to avoid
using them.

=====================================================================
               To find out who and where I am look at:
               http://www.nd.edu/~sholland/index.html

=====================================================================



Mon, 17 May 2004 16:23:37 GMT  
 Sum of two interger variables?


Quote:
>You should be careful about using $a and $b as variables since the
>Perl sort operator uses them.  Using $a and $b for non-sort things can
>cause problems if you are not very careful.  

What problems?

Quote:
>It's safer just to avoid using them.

Also, you should avoid walking under ladders, and never look at the
moon over your left shoulder.  It can cause problems if you are not
very careful.

rd
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print



Mon, 17 May 2004 17:19:34 GMT  
 Sum of two interger variables?

Quote:

> >You should be careful about using $a and $b as variables since the
> >Perl sort operator uses them.  Using $a and $b for non-sort things can
> >cause problems if you are not very careful.  
> What problems?
> >It's safer just to avoid using them.
> Also, you should avoid walking under ladders, and never look at the
> moon over your left shoulder.  It can cause problems if you are not
> very careful.

     Hmm... I see somebody woke up on the wrong side of the bed this
morning.  Do you really not understand why it is a bad idea to use $a
and $b if you don't even understand perl well enough to add two
integers?

=====================================================================
               To find out who and where I am look at:
               http://www.nd.edu/~sholland/index.html

=====================================================================



Mon, 17 May 2004 17:48:57 GMT  
 Sum of two interger variables?

Quote:

> Is it possible to have a interger variable, $a for example, add
> another interger variable to it, $b, and make the sum the new value of
> $a?

Yes but don't use $a or $b for variable names if you can help it.  They're
default names used in sort routines and may confuse things.

$onevariable += $secondvariable;

will increment $onevariable by the value in $secondvariable.

perldoc perlop for operators.

See also *=   /=    -=     %=

Hope this helps.

Graham Wood



Mon, 17 May 2004 18:08:49 GMT  
 Sum of two interger variables?

Quote:



>>You should be careful about using $a and $b as variables since the
>>Perl sort operator uses them.  Using $a and $b for non-sort things can
>>cause problems if you are not very careful.  

> What problems?

For example:

my $a = "string";

This will result in a fatal error since $a has been declared a lexical
before the sort-statement happens.

Quote:
>>It's safer just to avoid using them.

> Also, you should avoid walking under ladders, and never look at the
> moon over your left shoulder.  It can cause problems if you are not
> very careful.

;-)

But using $a _can_ cause problems, namely at compile-time.

Tassilo
--
When in trouble or in doubt,
run in circles, scream and shout.



Mon, 17 May 2004 20:05:45 GMT  
 Sum of two interger variables?

Quote:




>>>You should be careful about using $a and $b as variables since the
>>>Perl sort operator uses them.  Using $a and $b for non-sort things can
>>>cause problems if you are not very careful.  

>> What problems?

> For example:

> my $a = "string";


> This will result in a fatal error since $a has been declared a lexical
> before the sort-statement happens.

Ah, but MJD's comment was about using them in a non-sort context, which
in and of itself causes no problems.  If you're going to use it non-sort
*and* sort contexts...  :-)

At least I assume that's where he was aiming.

*shrug*

dha

--

"Perhaps *the* Monty python Scholar in America today"
        -New Yorker magazine 13mar89



Mon, 17 May 2004 23:01:54 GMT  
 Sum of two interger variables?


Quote:

>> >You should be careful about using $a and $b as variables since the
>> >Perl sort operator uses them.  Using $a and $b for non-sort things can
>> >cause problems if you are not very careful.  

>> What problems?

>  Do you really not understand why it is a bad idea to use $a
>and $b if you don't even understand perl well enough to add two
>integers?

That's right, I really do not understand.  I asked "what problems" and
I was hoping you would enlighten me.  What problems?

If you want to make an argument that such a person is better off
staying away from the comptuer entirely, I will not dispute it with
you.  If you had directed this person to a book about Perl, that would
make sense to me.

But I do not understand why a person who does not know the
addition operator needs a specific warning to stay away from $a and $b
and I do not understand why you think this would present any greater
problem than anything else.

rd
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print



Mon, 17 May 2004 23:30:14 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. How do I round a number to interger?

2. interger overflow like in C

3. HELP - converting string to interger

4. two variables, one target?

5. share variables across two forms

6. ? how 2 pass variables between two scripts

7. Reading two variables from a file

8. newbe question splitting a string into two different variables

9. Passing variables between two Perl scripts

10. Difference of two variables

11. calling two arrays in a function as variables

12. Printing two variables on alternate lines?

 

 
Powered by phpBB® Forum Software