LoginSignup
1
1

More than 5 years have passed since last update.

Perl6 Cookbook 一時変数を使用しないで変数の値を交換する

Last updated at Posted at 2015-11-17

一時変数を使わずに、変数の値を交換する。

Perl6の環境構築についてはこちら。
Perl6導入とHello, world! - Qiita

スカラーの値の交換

Perl5
use v5.18;

my ( $a, $b, $c ) = qw( あ い う );
say $a, $b, $c; #=> あいう


( $a, $b, $c ) = ( $b, $c, $a );
say $a, $b, $c; #=> いうあ
Perl6
use v6;

my ( $a, $b, $c ) = <    >;
say $a, $b, $c; #=> あいう


( $a, $b, $c ) = $b, $c, $a ;
say $a, $b, $c; #=> いうあ

配列の値の交換

Perl5
use v5.18;

my @array = ( 6 .. 9 );
say @array; #=> 6789

@array[0,1,2,3] = @array[2,3,1,0];
say @array; #=> 8976
Perl6
use v6;

my @array = 6 .. 9;
@array.join.say; #=> 6789

@array[0,1,2,3] = @array[2,3,1,0];
@array.join.say; #=> 8976

ハッシュの値の交換

Perl5
use v5.18;

my %hash = (
'大空' => 'あかり',
'瀬名' => '',
);
say %hash;

@hash{'大空', '瀬名'} = @hash{'瀬名', '大空'};
say %hash;
Perl6
use v6;

my %hash = '大空' => 'あかり',
           '瀬名' => '';
%hash.kv.join.say;

%hash{'大空', '瀬名'} = %hash{'瀬名', '大空'};
%hash.kv.join.say;

参考や注釈

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1