5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Perl のデータ構造を検索するためのモジュール Data::Search

Last updated at Posted at 2015-05-16

Data::Dumper とは

Data::Dumper はある変数のデータ構造を perl のシンタックスで出力してくれるモジュールです.

Data::Dumper を使うことでハッシュ,配列などが入った複雑な構造の変数を下記のように簡易に出力することができます.

sample01.pl
#!/usr/bin/env perl
# sample01.pl
use strict;
use Data::Dumper;
sub main {
  my $h = {
           'items' => [
                       {
                        'body' => "happiness is take a money",
                        'no' => 1,
                        'date' => '2014/11/09 14:15:03',
                        'stat' => 1,
                        'title' => "white-base thinking"
                       },
                       {
                        'body' => "happiness is playing beethoven",
                        'no' => 2,
                        'date' => '2014/11/19 12:35:23',
                        'stat' => 2,
                        'title' => "Hello World"
                       },
                      ]
          };

  print Dumper $h;
}
main;
% perl sample01.pl
$VAR1 = {
  'items' => [
   {
     'body' => 'happiness is take a money',
     'date' => '2014/11/09 14:15:03',
     'no' => 1,
     'stat' => 1,
     'title' => 'white-base thinking'
   },
   {
     'body' => 'happiness is playing beethoven',
     'date' => '2014/11/19 12:35:23',
     'no' => 2,
     'stat' => 2,
     'title' => 'Hello World'
   }
]
};

Data::Search モジュール

このようなハッシュ,配列からなる変数から「あるキーの値が○○なものを調べたい」,「この変数の値が××な部分はどうなっているのか調べたい」というときに下記の Data::Search モジュールが便利です.

sample02.pl
 #!/usr/bin/env perl
 # sample02.pl
 use strict;
 use Data::Dumper;
 use Data::Search;

sub main {
  my $h = {
           'items' => [
                       {
                        'body' => "happiness is take a money",
                        'no' => 1,
                        'date' => '2014/11/09 14:15:03',
                        'stat' => 1,
                        'title' => "white-base thinking"
                       },
                       {
                        'body' => "happiness is playing beethoven",
                        'no' => 2,
                        'date' => '2014/11/19 12:35:23',
                        'stat' => 2,
                        'title' => "Hello World"
                       },
                      ]
          };


  # body の値に money という文字列が入っているものを調べる
  my @out = datasearch( data => $h, 
                        search => "keys",
                        find => [ 'body' => qr/money/] ,
                        return => 'hashcontainer' );
  print Dumper \@out;
}

main;

上記のコードを実行すると,body に money という文字列を
含むハッシュが出力されます.

% perl sample02.pl
$VAR1 = [
  {
   'body' => 'happiness is take a money',
   'date' => '2014/11/09 14:15:03',
   'no' => 1,
   'stat' => 1,
   'title' => 'white-base thinking'
  }
]; 

ref

5
4
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
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?