Hello PriNet,
It seems the fellow Monks have provided you with solutions. I tried to retrieve the key without using grep but is seems that is more efficient to use it instead of not.
Sample of test code and Benchmark results:
#!/user/bin/perl use strict; use warnings; use feature 'say'; # use Benchmark qw(:all) ; # WindowsOS use Benchmark::Forking qw( timethese cmpthese ); # UnixOS my( %DetailSheet ) = ( 'ColumnName' => { 'ID' => { 'PositionAdjustment' => 0, 'RecordName' => 'Identification', 'RecordNumber' => 0, 'RecordValue' => '', 'XlsName' => 'Reference', 'XlsColumn' => 1, 'XlsFormat' => '', 'XlsWidth' => 15, 'XlsLock' => 1, 'GuiName' => 'Number', 'GuiNumber' => 2, 'GuiValue' => '', 'Switch' => [ ['O','0'], ['l','1'] ] }, 'DESCRIPTION' => { 'PositionAdjustment' => 0, 'RecordName' => 'Description', 'RecordNumber' => 1, 'RecordValue' => '', 'XlsName' => 'Description', 'XlsColumn' => 2, 'XlsFormat' => '', 'XlsWidth' => 15, 'XlsLock' => 1, 'GuiName' => 'Description', 'GuiNumber' => 3, 'GuiValue' => '', 'Switch' => [ [' AND ', ' & '], [' WITH ', ' W/ '] ] } } ); sub getKeyByIteration { my ( $DetailSheet ) = @_; for my $key ( keys %$DetailSheet ) { for my $secondLevelKeys ( keys %{ $DetailSheet->{$key}} ) { while (my ($key, $value) = each %{ $DetailSheet->{$key}{$secon +dLevelKeys} }) { return $secondLevelKeys if ($key eq 'XlsColumn' && $value == 2 +); } } } } # say getKeyByIteration(\%DetailSheet); sub getKeyByGrep { my ( $DetailSheet ) = @_; return grep { $DetailSheet{ColumnName}{$_}{XlsColumn} == 2 } keys %{$DetailSheet{ColumnName}}; } # say getKeyByGrep(\%DetailSheet); my $results = timethese(100000000, { Grep => \&getKeyByGrep, ForEach => \&getKeyByIteration, }, 'none'); cmpthese( $results ); __END__ $ perl test.pl Rate Grep ForEach Grep 1972776/s -- -51% ForEach 4011231/s 103% --
In reply to Re: grep confusion
by thanos1983
in thread grep confusion
by PriNet
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |