« [perl] やる夫が改行を取り除くようです。 | トップページ | やる夫がwordpressのサーバIPを変更したようです。 »

2008-07-16

[perl] やる夫が配列に配列を突っんだりサブルーチンに渡したりするようです。

サンプルプログラムの概要

main プログラムで、ハッシュを作成します。ハッシュ値”message”と”person_1”を作成し、一つはサブルーチンに飛ばして値をゲッチュします。

サブルーチン内では、配列の中にハッシュを突っ込みmainプログラムへ戻す内容となっています。

サンプルプログラム

#!/usr/local/bin/perl
### Data Storage
sub data_storage{

    my $sum_i = 3;
    my $sum_j;
    my @person;

    # It repeats three times.
    for($sum_j = 0; $sum_j < $sum_i; $sum_j++){

        # hush value setting
        my $name = 'yaruo';
        my $job  = 'good will';
        my $academic = 'graduate of a high school';
        # It is OK even if flying to the subroutine.

print "$sum_j _ $name hash data input \n";

        $person[$sum_j] = (
            {
                name      => $name,
                job       => $job,
                academic  => $academic,
            }
        );
    }

print "----- data out of hush  ----- \n";
print "$person[0]{name} \n";
print "$person[0]{job} \n";
print "$person[0]{academic} \n";
print "----- data out of hush  ----- \n";

    return \@person;

}

### main
my $storage;
$storage ->{message} = 'hash test';
$storage ->{person_1} = data_storage;

print "\n ---------- \n";
print $storage ->{message};
print "\n";
print $storage ->{person_1}[0]{name};
print "\n ---------- \n";

実行結果

# ./neet.pl
0 _ yaruo hash data input
1 _ yaruo hash data input
2 _ yaruo hash data input
----- data out of hush  -----
yaruo
good will
graduate of a high school
----- data out of hush  -----

----------
hash test
yaruo
----------
[root@blogapl351 perl]# ./neet.pl
0 _ yaruo hash data input
1 _ yaruo hash data input
2 _ yaruo hash data input
----- data out of hush  -----
yaruo
good will
graduate of a high school
----- data out of hush  -----

----------
hash test
yaruo
----------

|

« [perl] やる夫が改行を取り除くようです。 | トップページ | やる夫がwordpressのサーバIPを変更したようです。 »

コメント

コメントを書く



(ウェブ上には掲載しません)




トラックバック

この記事のトラックバックURL:
http://app.f.cocolog-nifty.com/t/trackback/488651/22330409

この記事へのトラックバック一覧です: [perl] やる夫が配列に配列を突っんだりサブルーチンに渡したりするようです。:

« [perl] やる夫が改行を取り除くようです。 | トップページ | やる夫がwordpressのサーバIPを変更したようです。 »