in reply to comparing two arrays

In the spirit of TIMTOWTDI:
use strict; my @report = qw(sqltable mike_test sarak_mike); my @file = qw(some other stuff mike_test sarak_mike); my %in_file = map{$_ =>1} @file; my $all_match = 1; foreach (@report) { if (! exists $in_file{$_}) { $all_match = 0; print "\@report value: '$_' is not in \@file.\n"; } } print "All values in \@report found in \@file\n" if $all_match;

--Jim

Replies are listed 'Best First'.
Re: Re: comparing two arrays
by jdelmedi (Initiate) on Nov 25, 2001 at 21:29 UTC
    I think I got overly excited - It's still not working The $_ in the hash seems to be blank and every run through comes back with the @report value does not exit in the @file. More help is surely appreciated.

    use strict;
    my $file='d:\mms_tableload.txt';
    my @file=();
    open(FILE,$file);
    @file = <FILE>;
    close(FILE);
    my @report = qw(sarak_mike mike);
    my %in_file = map{$_ =>1} @file;
    my $all_match = 1;
    foreach (@report) {
    if (! exists $in_file {$_} ) {
    $all_match = 0;
    print "\@report value: '$_' is not in \@file.\n";
    }
    }
    print "All values in \@report found in \@file\n" if
    $all_match;

      I've never seen the syntax  @file = ; (and I'm not doubting you, just curious) but are you checking to see what the contents of @file are before you enter the while loop? Also you should be sure to put  chomp @file; before the loop since you might have newlines in the file at the end of each record ("\n").

      Keep us posted.

      --Jim

      Afterthought: It is usually helpful when I'm debugging to use snippets similar to the following for testing arrays:

      my $i = 0; foreach (@file) { print "\@file[", $i++, "] = '$_'\n"; }
      It helps spot newlines, incorrect subscripting and such.

      Update: In case it is not clear from the previous posts, we're assuming that the file  'd:\mms_tableload.txt' looks like this:

      sarak_mike\n mike\n some\n other\n data\n
      The hash lookup must be an exact match (including newlines and case sensitivity). If there is other extraneous information like HTML tags embedded in @file, then you may need to use a different method that searches strings (like grep, or m/$_/), and this can be tricky since you'd have to insure that your search for "mike" doesn't match positve to "sarak_mike". </code>
        Sorry for not providing all the info, but the file looks like this,

        Insp_data table loaded successfully 2001-11-26 00:34:58.61 with 284734 records.
        Inventory_trans_temp table loaded sucessfully 2001-11-26 01:31:08.88 with 498 records.
        Invoice_extension table loaded successfully 2001-11-26 01:37:07.85 with 165287 records.
        Invoice_header table loaded successfully 2001-11-26 01:45:04.79 with 165321 records.

        My objective is to locate and compare the table names in the file with an array which may possibly conatin these same names.

Re: Re: comparing two arrays
by jdelmedi (Initiate) on Nov 25, 2001 at 01:26 UTC
    Now it made sence!!!!!! It workded in the real world, Hats off to you Sir!!!
      Understand that dupes are killed when hash is built. Though you did not specify that duplication of values is important, this test does not tell you that you have the same number of dupes of a given record in both record sets. Move SIG!
        You are right. I am still having a problem. The code does not recognise the values in @record although present in @file. When I try to print the $_ in the hash it's blank. Suggestions are welcome - I think I got overly excited --

        my $file='d:\mms_tableload.txt'; my @file=(); open(FILE,$file); @file = <FILE>; close(FILE); my @report = qw(sarak_mike mike); my %in_file = map{$_ =>1} @file; my $all_match = 1; foreach (@report) { if (! exists $in_file {$_} ) { $all_match = 0; print "\@report value: '$_' is not in \@file.\n"; } } print "All values in \@report found in \@file\n" if $all_match;