sironekotoroの日記

Perl と Mac の初心者の備忘録

Perl言語プログラミングレッスン[入門編] 第4章

4-2

use strict;
use warnings;

my $time = '01:23:45';

my @array = split( ':', $time );

my ( $hour, $min, $sec ) = @array[ 0, 1, 2 ];

print "$hour$min$sec\n"

それに対する結城先生の回答

my ( $hour, $min, $sec ) = split(/:/ , $time);

鮮やかすぎ!

4-3

my $last = @array;
if ( 0 <= $index, $index <= $last ) {
    print "$array[$index]" . "\n";
}

それに対する結城先生の回答

if ( 0 <= $index, $index <= @array ) {
    print "$array[$index]" . "\n";
}

比較演算子で、@arrayがスカラーで評価されるから?

4-4

use strict;
use warnings;

#(1)
my @array = ( 0 .. 10 );
my $item  = 20;

push( @array, $item );
print "@array" . "\n";

@array = ( 0 .. 10 );

splice( @array, $#array, 0, $item );
print "@array" . "\n";


#(2)
@array = ( 0 .. 10 );

$item = pop(@array);
print "$item" . "\n";

@array = ( 0 .. 10 );

$item= splice( @array, $#array, 1, );
print "$item" . "\n";


#3
@array = ( 0 .. 10 );

$item = shift(@array);
print "$item" . "\n";

@array = ( 0 .. 10 );

$item = splice( @array, 0, 1 );
print "$item" . "\n";

#4
@array = ( 0 .. 10 );
$item  = 20;
unshift( @array, $item );
print "@array" . "\n";

@array = ( 0 .. 10 );
$item  = 20;
splice( @array, 0, 0, $item );
print "@array" . "\n";

#5
@array = ( 0 .. 10 );
$item  = 20;
my $index = 3;

$array[$index] = $item;
print "@array" . "\n";

@array = ( 0 .. 10 );
$item  = 20;
my $index = 3;

splice( @array, $index, 1, $item );
print "@array" . "\n";

4-6

use strict;
use warnings;

my @info = (
    'hyuki@hyuki.com:Hiroshi Yuki:37',
    'hanako@hyuki.com:Hanako Sato:23',
    'tomura@hyuki.com:Tomura:22'
);

print "<html><table border=\"1\">\n";

foreach (@info) {

    my @array = split( /:/, $_ );
    @array = join '</td><td>', @array;
    print '<tr><td>' . "@array" . '</td></tr>' . "\n";

}

print "</table></html>\n"

分割しながら変数に入れていくってのがどうにも思いつかないもんだなぁ

my ($mail,$name,$age) = split( /:/, $_ );

4-7

use strict;
use warnings;

my $input = 7;

my @kannumber=qw/〇 壱 弐 参 四 五 六 七 八 九 拾/;

print $kannumber[$input];

4-8

use strict;
use warnings;

my @array = (0..9);

my @koujyun = sort { $b <=> $a} @array;

print "@koujyun" . "\n";

4-9

use strict;
use warnings;

my ( $myname, $mymsg, $mytime ) = ( 'sironekotoro', 'nora-cat', '2013-11-23' );

my $data = "$myname,$mymsg,$mytime";
print $data . "\n";

#(1)
my $character = join ',' , $myname ,$mymsg , $mytime;
print $character . "\n";

#(2)
my @bunkatu = split /,/ , $data;
foreach(@bunkatu){
    print "$_\n";
}