Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^3: correspondence between two arrays

by choroba (Bishop)
on Nov 24, 2011 at 10:11 UTC ( #939841=note: print w/replies, xml ) Need Help??


in reply to Re^2: correspondence between two arrays
in thread correspondence between two arrays

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).

Replies are listed 'Best First'.
Re^4: correspondence between two arrays
by anasuya (Novice) on Nov 24, 2011 at 10:15 UTC

    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.

      the thing is i want the results with respect to the entries in file1.txt
      That is exactly what you get if you hash the second file and iterate over the first one.

      When you say you need to "check whether a value exists in something," that almost always means you should put something in a hash, and then check your values against that hash. Since you want to check whether values from file1 exist in file2, you put file2 in a hash (with the part you're checking against as the keys), and then start looping through file1 and checking its values to see if they exist as keys in the hash.

      Aaron B.
      My Woefully Neglected Blog, where I occasionally mention Perl.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://939841]
help
Chatterbox?
[LanX]: it's considered spamming to advertise threads here
[LanX]: just wait

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2018-04-24 15:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?