Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Using system (); with Strawberry Perl

by Marshall (Canon)
on Nov 25, 2021 at 10:09 UTC ( [id://11139113]=note: print w/replies, xml ) Need Help??


in reply to Using system (); with Strawberry Perl

jwkrahn has the right idea. Instead of using Perl to call an O/S specific function, write Perl code that runs on multiple platforms: Windows, Cygwin or Unix.

Perhaps some code like this code for your first system call with grep?
I did not create the test files necessary to actually prove that this works on my Windows system, but this is plausible.

#!/usr/bin/perl use strict; use warnings; use autodie; #system ('grep -l "DATAmessage.*3\.0" *.xml > 3.0_files_arraydata.txt' +); open my $OUT, '>', '3.0_files_arraydata.txt'; foreach my $filename (<*.xml>) { open my $in, '<', $filename; print $OUT "$filename\n" if grep{/DATAmessage.*3\.0/}<$in>; }
The above code will be slower than Unix grep -l because this code looks at every line of the input file and reports the number of lines that matched, and if >0, that fact causes the filename to be printed. grep -l stops at the first matching line and reports the file name. Speed depends upon how big your files are. A couple more lines of Perl code can emulate the exact grep -l functionality (stop reading the file when the first match is found). I have no idea what File::Grep is and why you would need it. Perl regex is very fast. Literally decades of tweaking have gone into the regex engine.

Anyway, try the above out and see how it goes.

Replies are listed 'Best First'.
Re^2: Using system (); with Strawberry Perl
by hippo (Bishop) on Nov 25, 2021 at 10:34 UTC
    A couple more lines of Perl code can emulate the exact grep -l functionality (stop reading the file when the first match is found).

    If you want the speed that grep -l would give you then have a look at List::Util::any instead. It has an XS version and is in Core.


    🦛

Re^2: Using system (); with Strawberry Perl
by hadrons (Novice) on Nov 25, 2021 at 19:15 UTC
    File::Grep was slow because I used fgrep instead of just grep ... I'll test out your code later in the day when I have enough test files
      This code print $OUT "$filename\n" if grep{/DATAmessage.*3\.0/}<$in>; is slow because it continues to read the file even after it has found the first occurrence of the regex match (it actually counts the number of occurrences in the file). To use hippo's idea: add use List::Util qw(any); at the top. And change code to print $OUT "$filename\n" if any{/DATAmessage.*3\.0/}<$in>;.

      the "any" routine is written in C. The Perl equivalent is like this:

      while (<$in>) { if (/DATAmessage.*3\.0/) { print $OUT "$filename\n"; last; #no need to look anymore! } }
      If whatever you are looking for usually appears near the beginning of the file, performance gain will be substantial.

      update:
      Another place to use a List::Util function:

      { my %unique; print $OUT sort grep { ! $unique{ $_ }++ } <$IN>; } ##### again use List::Util to speed up Perl implementation... #### use List::Util qw(any uniq); print $OUT sort uniq <$IN>;
      I suppose that depending upon the data, it could be that reversing the order, i.e., sorting and then filtering out uniq lines would be faster? Don't know. But if speed is needed, I would also benchmark that approach. Also, instead of building a hash table, try: "print line unless its a repeat of previous line". Results probably depend upon what typical data actually looks like. For example:
      my $prev = ""; foreach (sort <$IN>) { print unless $_ eq $prev; $prev = $_; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11139113]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (8)
As of 2024-04-19 07:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found