in reply to Re: Compare Arrays, Return Values
in thread Compare Arrays, Return Values

That's about the sum of it. I just am at a complete loss for what to do.

Replies are listed 'Best First'.
Re^3: Compare Arrays, Return Values
by gone2015 (Deacon) on Aug 28, 2008 at 16:09 UTC

    OK. Well, first use: (a) use strict; (b) use warnings; (c) my variables.

    Now, in your input loop you have:

    @t = split(/\t/, $_) ; $t[4]= $AB{[4]} ;
    which is a horrible mess :-( What you want to do is:
    $AB{$alpha_numeric_field} = $numeric_field ;
    where the two fields are elements of @t, I cannot tell which.

    The output process contains:

    if ($var1[2] and exists $Ab{$var1[2]}) { print OUT "$var1[0]\t$var1[1]\t$var1[2]\t$AB[4]\n" ; }
    which looks as though it would be OK, but for $AB[4], which ought to be $AB{$var1[2]}. (Oh. And it should be $AB{...} in the if !).

    use strict would have picked up the $Ab{} mistake (undefined hash %Ab) and the use of $AB[4] (undefined array @AB). It would not, however, have picked up $AB{[4]}, which is the kind of honest-to-goodness inscrutability that Perl is justly famous for, and which one grows to be deeply fond of.

    BTW, rather than:

    @var1 = split(/\t/,$_) ;
    you can write (for example):
    ($field_name, $other_field, $further_field, ...) = split(...) ;
    then you have each field in a variable with a meaningfull name -- more friendly than $var1[2] etc.

      Thank you so very much!!! This worked like a charm. I cannot tell you how greatful I am. I knew it had to be something simple that my brain wasn't registering for some reason. Thank you again.
Re^3: Compare Arrays, Return Values
by apl (Monsignor) on Aug 28, 2008 at 15:20 UTC
    I don't mean to be rude, but you've just described the problem. How can you be at a loss as to what to do?

    Open file 1
    • For each record in file 1
      • break the record into fields
      • update a hash using Product Number and Produce Name as the keys, with a value of Univers ID

    Open file 2
    • For each record in file 2
      • break the record into fields
      • Is there a hash with Product Number and Produce Name as the keys?
        • If so, write out the keys and the value
      I understand what you're saying but I still am unsure as to how the appropriate "UniversalID" values will be written along with the "ProductID" values. That is where my confusion is coming in.
        minor update: I forgot to give the necessary admonition about strict and warnings, but I see that dwm042 has covered that below.

        Here's some code that does basically what apl is suggesting (in the spirit of getting you started, but leaving some things for you to do to enhance the learning experience):

        #! /usr/bin/perl use strict; use warnings; my @file1 = ( 'A101 something 123', 'A102 else 456', 'A103 it 789', 'A104 the 012' ); my @file2 = ( 'A102 else 456', 'A103 it 789' ); my %prod_id; foreach my $line ( @file1 ) { my( $num, $name, $id ) = (split /\s+/, $line); $prod_id{$num} = $id; } foreach my $line ( @file2 ) { my( $num, $name ) = (split /\s+/, $line); print "$num\t$name\t$prod_id{$num}\n"; }

        There is still a lot of work that you need to do with this. For example, to make it easy on myself, I take sample data from arrays, not files (you will need to fix that part). I also took some shortcuts by (1) always ignoring ProductName, (2) always assuming that the relevant ProductID exists as a key in the hash (this really needs to be checked at the point where I use $prod_id{$num} above).

        As I said, this is not offered as a complete solution, rather as something to get you started.