Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re^2: correspondence between two arrays

by anasuya (Novice)
on Nov 24, 2011 at 09:57 UTC ( #939836=note: print w/replies, xml ) Need Help??

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

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

Replies are listed 'Best First'.
Re^3: correspondence between two arrays
by choroba (Bishop) on Nov 24, 2011 at 10:11 UTC
    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.

        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.

Re^3: correspondence between two arrays
by cavac (Deacon) on Nov 24, 2011 at 11:45 UTC

    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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://939836]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2018-02-24 22:47 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (311 votes). Check out past polls.