perl hashes - comparing keys and values

Posted by Aaron Moodie on Stack Overflow See other posts from Stack Overflow or by Aaron Moodie
Published on 2010-04-18T07:47:30Z Indexed on 2010/04/18 7:53 UTC
Read the original article Hit count: 320

Filed under:
|

I've been reading over the perl doc, but I can't quite get my head around hashes. I'm trying to find if a hash key exists, and if so, compare is't value. The thing that is confusing me is that my searches say that you find if a key exists by if (exists $files{$key}) , but that $files{$key} also gives the value? the code i'm working on is:

foreach my $item(@new_contents) {
        next if !-f "$directory/$item";
        my $date_modified = (stat("$directory/$item"))[9];

        if (exists $files{$item}) {
            if ($files{$item} != $date_modified {
                $files{$item} = $date_modified;
                print "$item has been modified\n";
            }
        } else {
            $files{$item} = $date_modified;
            print "$item has been added\n";
        }
    }

© Stack Overflow or respective owner

Related posts about perl

Related posts about beginner