in reply to correspondence between two arrays

"bt i am not able to get it into proper code"

Post the code you have which doesn't work the way you want.

Replies are listed 'Best First'.
Re^2: correspondence between two arrays
by anasuya (Novice) on Nov 24, 2011 at 09:57 UTC
    open AD, "file1.txt"; @arr=<AD>; foreach $line(@arr) { $name{substr($line,0,4)}++; } close AD; #hash out of the contents of the first file=MADE #----------- open FH, "file2.txt"; @arr2=<FH>; foreach $a(@arr2) { @val=split(' ',$a); foreach $v($#val) if (...what do i do here???..) {print..... } }
      You said
      for each value in file1, i have to check whether that value exists in second field of file2
      But you iterate over @arr2 in your code. It might be better to hash the second file, and then go through the first one and print the corresponding hashed value, if it exists, or NIL otherwise.

      You might need to chomp the lines if you do not want the \n included in your strings (not needed if the key is always the last thing on any line).

        the thing is i want the results with respect to the entries in file1.txt and not the other way around. so if i make the second file as hash, then wont that make the prblem exactly opposite? as for the iteration, i am sorry i think i missed a bracket } there.. sorry for the mistake. Please tell me wat to do.

      For starters, you don't check if open() actually worked. That's not good. You should also use strict; and use warnings; too.

      You should also use a consistent coding and indenting style. Doing so will help you see the logic flow of your own code. perltidy can help you with that.

      This may sound like i'm picking out the "unimportant" parts of software design. Well, actually, no i'm not. First you have to get the basics right. Then, the tools you use can actually help you in developing your script by telling you about certain problems you might have overlooked.

      Don't use '#ff0000':
      use Acme::AutoColor; my $redcolor = RED();
      All colors subject to change without notice.