Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: If Exists in an Array?

by bichonfrise74 (Vicar)
on Nov 12, 2009 at 23:07 UTC ( [id://806823]=note: print w/replies, xml ) Need Help??


in reply to If Exists in an Array?

Note: untested. I think this is something that you might want to take a look at. Again, untested.
#!/usr/bin/perl use strict; my $logfile = '/tmp/WDL_Scan_0000.csv'; my $locofile = '/tmp/locos.txt'; my ($locodn, $loco_data); my ($linecnt1, $linecnt2) = 0; open( my $file_1, '<', $logfile) or die "Error: Cannot open $logfile."; my @loco_data = <$file_1>; close( $file_1 ); open( my $file_2, '<', $locofile) or die "Error: Cannot open $locofile."; while ( <$file_2> ) { chop($locodn); my ($RR_Name, $Loco_no, $Event_Date, $Event_Time, $Event_Code, $Event_Duration,$Source_File ) = split( /,/ ); chop($loco_data); chop($Loco_no); $linecnt1++ if grep { /\b$Loco_no\b/ } @loco_data; $linecnt2++; print "Rec: ",$linecnt2, " In: ",$linecnt1, "\r"; } close( $file_2 );

Replies are listed 'Best First'.
Re^2: If Exists in an Array?
by 7stud (Deacon) on Nov 13, 2009 at 10:16 UTC
    my ($linecnt1, $linecnt2) = 0;

    Is that what you meant to write?

    linecnt1++ if grep { /\b$Loco_no\b/ } @loco_data;

    Doing a hash lookup--like the op was trying to do--is more efficient than searching the entire array every time through the loop. You know, O(n squared) v. O(n) type stuff.

    use strict; use warnings; use 5.010; open (my $LOGFILE, "<", "logfile.txt"); open (my $MATCHFILE, "<", "matches.txt"); chomp(my @keys = <$MATCHFILE>); close $MATCHFILE; #Initialize hash: my %target_matches; @target_matches{@keys} = (); #now the keys exist, and the values are undef my($num_records, $match_count) = (0, 0); while (<$LOGFILE>) { chomp; my @fields = split /,/; if (exists $target_matches{$fields[1]}) { $match_count++; } $num_records++; say "Total records: $num_records, matches: $match_count"; } close $LOGFILE;

    matches.txt:

    1 2 3 4

    logfile.txt:

    a,5,b,c,d,e a,2,b,c,d,e a,8,b,c,d,e a,3,b,c,d,e

    output:

    Total records: 1, matches: 0 Total records: 2, matches: 1 Total records: 3, matches: 1 Total records: 4, matches: 2
      I prefer:
      my %target_matches = map { chomp; $_,undef } <$MATCHFILE>;

      No need for @keys that way.

      Note: untested.

      Ah.
      Thanks so much - this is going to work great. I have one other question. My source file just because of the way it is built - the $field1 value has leading spaces, how can I remove them on the fly?
      Source Example: sx, 8849, sample1, source2 st, 893, sample2, source3 Matching Table: 8849 893
      With the leading white space the match does not hit. Thanks Again, Ad.
        Assuming that your example pertains to this:
        ($RR_Name,$Loco_no,$Event_Date,$Event_Time,$Event_Code,$Event_Duration +,$Source_File)=split(/,/,$record);

        You're currently splitting on just the comma, so the spaces are retained. To lose the lead/trail spaces just change your split:

        ($RR_Name,$Loco_no,$Event_Date,$Event_Time,$Event_Code,$Event_Duration +,$Source_File)=split(/[ ,]+/,$record); # + ^ ^^

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-25 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found