in reply to Re^5: Comparing files
in thread Comparing files

I have read learning perl 2 times. I also have programming perl by larry wall as well. Where I think I am confused is, I thought a hash had to have a key and a value. How do I get my mac addresses to just be keys and if they are the keys what are the values then, i'm assuming undef. What I need to know is, how to write a file into the keys of a hash. I know how to open the file using the filehandle, from there is what I cant figure out.

Replies are listed 'Best First'.
Re^7: Comparing files
by davido (Cardinal) on Jul 20, 2004 at 18:03 UTC
    Oh, ok, that's great. :)

    Actually, hashes don't necessarily need to have values assigned to the elements. But in your case, it doesn't matter; either assign a value or don't. Here's an example:

    my %hash; while ( <DATA> ) { chomp; $hash{$_} = 1; }

    Or even:

    my %hash = map { chomp; $_ => undef } <DATA>;

    If you need to massage each line so that you have only the mac address, that is pretty easy to do too. The second example does slurp the file, so beware if your files are massive. Of course you'll have to have at least enough resources to hold all the mac addresses in memory anyway.

    After reading that first file, and creating your hash, you can go through and use the hash values as flags to mark on each subsequent file if the mac address is found. At the end of each file read, check to see if you've flagged all hash keys, and each hash element that doesn't have its flag turned on, you can delete. Then reset all flags (or use a different flag for the next file), and so on.

    It's just one way.


    Dave