14

I want to swap two variables values without using the third variable in Perl, e. g.:

my $first = 10;
my $second = 20;

Please suggest me how we can do this in Perl in a simple way.

Vadim Pushtaev
  • 2,332
  • 18
  • 32
  • ($first, $second)=($second, $first); This is technically cheating your rules since it does create an anonymous array... – abiessu Sep 04 '13 at 05:45
  • 6
    @abiessu: Nope, no anonymous array. (They're just lists; for an anonymous array, you'd use `[...]` to get an arrayref.) And even if there *were* an anonymous array, I don't think I'd count it as a "third variable". – ruakh Sep 04 '13 at 05:49

7 Answers7

24

The best way that only provide us is like this in one line you can swap the values:

 ($first, $second) = ($second, $first);
brian d foy
  • 129,424
  • 31
  • 207
  • 592
Developer
  • 6,292
  • 19
  • 55
  • 115
  • Yup but i will suggest u to dis in a proper manner.I am updating my answer – Developer Sep 04 '13 at 05:49
  • 2
    Your version with `my` technically isn't swapping the values of existing variables, but rather creating new variables whose names shadow the existing ones. – ruakh Sep 04 '13 at 05:51
  • 2
    Why do you say that your second version is the best way to do it? The second version is really obscure. Your first version is much clearer. I'd use that every time. – Dave Cross Sep 04 '13 at 10:18
  • I think that logically this is the best way to this.You can implement that code any where .this was the reason i thought..Thanks – Developer Sep 04 '13 at 10:20
  • 2
    The question said "in Perl". Your first answer is clearly the best way to do it in Perl. – Dave Cross Sep 04 '13 at 10:31
23

You can write:

($first, $second) = ($second, $first);

(See §3.4 "List Assignment" in Learning Perl, Third Edition.)

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • 2
    We're up to the sixth edition of [Learning Perl](http://www.learning-perl.com) now :) – brian d foy May 19 '15 at 15:51
  • 2
    Are you saying people weren't really paying attention to the first five? Because from the look of the other answers... ;-) – T.Rob May 19 '15 at 17:32
-1

The Perl-specific methods already listed are best, but here's a technique using XOR that works in many languages, including Perl:

use strict;

my $x = 4;
my $y = 8;

print "X: $x  Y: $y\n";

$x ^= $y;
$y ^= $x;
$x ^= $y;

print "X: $x  Y: $y\n";

X: 4  Y: 8
X: 8  Y: 4
  • 5
    This won't work in Perl since the `^` operator has slightly different semantics in string and number forms. Change one of your values to a non-numeric string and you'll see the problem. – brian d foy May 19 '15 at 15:45
  • ...and it won't work if $x == $y, at least if they're numeric. – Jan Nov 13 '15 at 06:34
-4

You can do this relatively easy using simple maths.

We know;

First = 10
Second = 20

If we say First = First + Second

We now have the following;

First = 30
Second = 20

Now you can say Second = First - Second (Second = 30 - 20)

We now have;

First = 30
Second = 10

Now minus Second from First, and you get First = 20, and Second = 10.

serenesat
  • 4,611
  • 10
  • 37
  • 53
-5
$first = $first + $second;
$second = $first - $second;
$first = $first-$second;

This will swap two integer variables A better solution might be

$first = $first xor $second;
$second = $first xor $second;
$first = $first xor $second;
choroba
  • 231,213
  • 25
  • 204
  • 289
rove101
  • 11
  • 2
  • 1
    This also assumes that the variables contain data that is addable/subtractible like numbers. – abiessu Sep 04 '13 at 05:53
  • 3
    Re: "This will swap two integer variables in any programming language": That's a bit too broad, I think. Even if we restrict consideration to languages that allow you to assign new values to existing variables, this won't work for all integer values in languages where integer overflow isn't well-defined. (For example, in C this code is dangerous.) – ruakh Sep 04 '13 at 05:54
  • okk..my mistake. I did not consider integer overflows. But I think there will always a range for integers in any language. Please point out my mistake if i am wrong. anyways i have edited the answer. thanks – rove101 Sep 04 '13 at 05:59
  • 2
    Far too complicated. Why not just `($first, $second) = ($second, $first)`? – Dave Cross Sep 04 '13 at 10:19
  • @DaveCross Can you tell me how is this far too complicated? I mean the way you gave looks simple, but is complicated internally. What is wrong in giving a logical answer? Anyways before voting down please do give reasons as well – rove101 Sep 04 '13 at 13:01
  • 2
    You can see it's far too complicated just by looking at it. It takes three statements where the list assignment only takes one. The list assignment shows exactly what is happening (two variables are swapping their values) whereas in your version the main point of the process only happens as a side-effect of some arithmetic. Ask yourself whether a random programmer would be able to understand what's going on from just looking at your code. And finally, your code doesn't even solve the whole problem. The question just mentioned "variable values". Your solution only works for numbers. – Dave Cross Sep 04 '13 at 13:17
  • 2
    This won't work since you'll destroy the non-numeric string value of a scalar. It also will possibly change floating point values. – brian d foy May 19 '15 at 15:48
-6

you can use this logic

firstValue = firstValue + secondValue;

secondValue = firstValue - secondValue;

firstValue = firstValue - secondValue;
serenesat
  • 4,611
  • 10
  • 37
  • 53
Android dev
  • 273
  • 2
  • 5
  • 23
-6
#!/usr/bin/perl

$a=5;
$b=6;

print "\n The value of a and b before swap is --> $a,$b \n";

$a=$a+$b;
$b=$a-$b;
$a=$a-$b;

print "\n The value of a and b after swap is as follows:";
print "\n The value of a is ---->$a \n";
print "\n The value of b is----->$b \n";
drs
  • 5,679
  • 4
  • 42
  • 67
  • 2
    This won't work since you'll destroy the non-numeric string value of a scalar. It also will possibly change floating point values. – brian d foy May 19 '15 at 15:47