Skip to content

Instantly share code, notes, and snippets.

@Code-Hex
Created August 20, 2014 04:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Code-Hex/b578667c7e7af5c82c5d to your computer and use it in GitHub Desktop.
Save Code-Hex/b578667c7e7af5c82c5d to your computer and use it in GitHub Desktop.
push,popとshift,unshiftについて
#! /usr/bin/perl
use strict;
use warnings;
my @array = qw/1 2 3 4 5 6 7 8/;
print "first!!\n";
print "$_ " for (@array);
print "\n"x2;
my $a = pop @array; # 配列の一番最後の配列を抜き出す
print "pop!!\n";
print "$_ " for (@array);
print "\n"x2;
push @array, $a;
print "push!!\n";
print "$_ " for (@array);
print "\n"x2;
print "pop,pushと同じようにshift,unshiftも使えるけど少し違う!\n\n";
print "first!!\n";
print "$_ " for (@array);
print "\n"x2;
my $b = shift @array; # 配列の先頭の配列を抜き出す
print "shift!!\n";
print "$_ " for (@array);
print "\n"x2;
unshift @array, $b;
print "unshift!!\n";
print "$_ " for (@array);
print "\n";
= 実行結果
first!!
1 2 3 4 5 6 7 8
pop!!
1 2 3 4 5 6 7
push!!
1 2 3 4 5 6 7 8
pop,pushと同じようにshift,unshiftも使える!
first!!
1 2 3 4 5 6 7 8
shift!!
2 3 4 5 6 7 8
unshift!!
1 2 3 4 5 6 7 8
=cut
@Code-Hex
Copy link
Author

初めての人は困惑するよね...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment