in reply to help with search and match

This smells very strongly of homework.

What have your tried? Show us your code.

(Hint: Use a hash. Look it up in your Perl text, it should describe what it is and how it works.)

--t. alex
but my friends call me T.

  • Comment on Re: help with search and match (Homework?)

Replies are listed 'Best First'.
Re: Re: help with search and match (Homework?)
by Anonymous Monk on Aug 26, 2002 at 17:07 UTC
    this is actually not school related....anyways, i tried this:
    open(FILE1, "$file1") || die "can't open file"; open(FILE2, "$file2") || die "can't open file"; while (<FILE2>) { chomp; @array2= split; while(<FILE1>) { chomp; @array1 = split; } if ($array1[0] eq $array2[0]) print "match\n"; if ($array1[1] == $array[2]) { print $array[1]; } else { print " no match"; } }
      I can see a number of problems with your code ..
      • The first time through the FILE2 loop, you'll read all of FILE1 and it will come up empty after that.
      • You are reading all of FILE1 for each line of FILE2 -- that's not efficient.
      • I would rename your array variables as array1 and array2 (corresponding, of course, to FILE1 and FILE2)
      Second to last, I would reformat the code as:
      open(FILE1, "$file1") || die "can't open file"; open(FILE2, "$file2") || die "can't open file"; while (<FILE2>) { chomp; @array2= split; while(<FILE1>) { chomp; @array1 = split; } if ($array1[0] eq $array2[0]) { print "match\n"; if ($array1[1] == $array[2]) { print $array[1]; } else { print " no match"; } } }
      Finally, check out the other good responses to the original question .. your approach should be abandoned.

      --t. alex
      but my friends call me T.

      Also, use strict; use warnings; to catch some of your typos (and declare your variables).
      This looks alot like a question from a few days ago. See my response in that thread.

      --

      There are 10 kinds of people -- those that understand binary, and those that don't.