スポンサーリンク

Perlで連想配列の要素の数を取得する覚え書き

使う機会があるかすらわからないけど、Perlで連想配列の要素数を取得する方法を考えてみました。

と言っても、keys でキーの配列を作ってスカラーに代入するだけなんですけどね(^_^;)。

#!/usr/bin/perl

use strict;
use warnings;

my %hash=("a"=>1, "b"=>1, "c"=>1);

my $hashLen = keys %hash;

print $hashLen,"\n"; # 3が表示されます。
 

もしくはscalar関数で評価しちゃうとか・・・。

#!/usr/bin/perl

use strict;
use warnings;

my %hash=("a"=>1, "b"=>1, "c"=>1);

print scalar keys %hash,"\n"; # 3が表示されます。
 

Enjoy!

スポンサーリンク
コメント
Verify your Comment
Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

コメントを投稿

プロフィール


  • 書いてる人:夢界 陸

    名古屋在住のおっさん。
    プログラミングやガジェットの話など、 日々の興味を徒然と綴っています。



    Twitterやってます @mukairiku

    運営サイト
    www.mukairiku.net

ブログ内検索

Licenses

  • Creative Commons License

OTHER

  • このブログのはてなブックマーク数

Blog powered by TypePad

スポンサーリンク