in reply to grep confusion

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% --
Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re^2: grep confusion
by PriNet (Monk) on Sep 03, 2017 at 00:30 UTC
    I did finally get the grep to work as expected thanos1983, again, like I mentioned above, I'm begining to think my environment for perl is causing gremlins, I may fire up an old windows xp machine by itself to do my testing on, I'm begining to think the virtual machine's limitations are being exceeded... previously correct/good/executable lines in my code that worked before I kept adding more script are starting to throw errors... thanx again everyone for your guidance, maybe someday I can actually help someone else *heh*
    -Gary

    I tried re-inventing the wheel again, but everytime I push it, it still falls flat on it's side...
      I believe I found out what's causing my 'arbitrary' errors (they only happen sometimes). So here's an update and I welcome any resolutions anyone may have:
      I use pdf2text to convert some (work) files to an xls worksheet
      everything was going fine until I wrote in an 'opportunity' to edit the XLS file before continuing on with my script.
      I use Spreadsheet::WriteExcel; to create the initial XLS (works fine)
      I then use
      system('C:/Progra~1/OpenOf~1/Program/Scalc "'.$CurrentDirectory.'/Resources/'.$Filename.'"');
      (which also works fine) to start OpenOffice for the 'editing' option
      then directly after that I (using)
      use Spreadsheet::ParseExcel::SaveParser;
      start reading the file with the line:
      $XlsWorkbook = Spreadsheet::ParseExcel::SaveParser->new()->Parse($CurrentDirectory.'/Resources/'.$Filename);
      to 'reload' the file to update my 'optional' changes...
      I am under the impression (because it only happens 'sometimes') that when I close the file in OpenOffice, my script does not 'wait' long enough for the file to completely close in OpenOffice... thus I end up trying to read cells that haven't been defined yet because OpenOffice hasn't finished closing the file.
      I am (of course) going to play with 'sleep' to see if I can slow things down in my script, but, I read in several places that 'sleep' is kinda dangerous in scripts.
      Here's the question: is there a way to tell when OpenOffice has actually completed the close operation before trying to read the file again ?
      I have looked around and found that the 'system' command I use can be modified for different actions and found this:
      system(`nmon -F file.out -s3 -c1`);
      I'm not fluent with the 'system' command and I can't seem to find much information on 'system' command options on the net, should i attempt to play around with this suggestion to see if I can make it work ? -or- is there a module somewhere that I can use to 'watch' OpenOffice' and ensure the XLS file has completely finished/closed/saved before trying to read in the 'changes'?
      I REALLY hope I explained things well enough here. I'll be holding a tin can out for anyone's two-bits...

      I tried re-inventing the wheel again, but everytime I push it, it still falls flat on it's side...

        PriNet:

        You could use stat to get the file modification time, and then sit in a loop waiting for the file modification time to change:

        $ cat pm_1198604.pl use strict; use warnings; system("touch FName.dat"); my $ctime = (stat 'FName.dat')[10]; # Proxy for your spreadsheet update... system("(sleep 10; date; touch FName.dat) &"); my $timeout = time + 60; # Don't wait forever! while (1) { # File changed yet? my $new_ctime = (stat 'FName.dat')[10]; last if $new_ctime > $ctime; die "TIMEOUT!" if time > $timeout; print "still unchanged, waiting a little while\n"; sleep 2; } print ".. continuing to next chunk of work\n"; $ perl pm_1198604.pl still unchanged, waiting a little while still unchanged, waiting a little while still unchanged, waiting a little while still unchanged, waiting a little while still unchanged, waiting a little while still unchanged, waiting a little while Sun, Sep 3, 2017 11:35:11 AM .. continuing to next chunk of work

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.