How can I swap two Perl variables without using the third?

后端 未结 7 984
盖世英雄少女心
盖世英雄少女心 2021-01-17 13:27

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

7条回答
  •  难免孤独
    2021-01-17 13:52

    #!/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";
    

提交回复
热议问题