http://www.perlmonks.org?node_id=653952


in reply to Re^2: Mulitple values for a key in a hash
in thread Mulitple values for a key in a hash

In case that you need hash key existance (and not only) check use exists.
my %filesystems = ( hosta => { desc => 'x', filesystemA => qq|/the/first/filesystem|, +filesystemB => qq|/the/second/fielsystem| }, hostb => { desc => 'r', filesystemA => qq|/the/first/filesystem|, f +ilesystemB => qq|no filesystemB for 2nd host|} ); print "filesystemA exists\n" if exists $filesystems{'hosta'}->{'filesystemA'}; print "no_such_fs does not exist\n" if not exists $filesystems{'hosta'}->{'no_such_fs'};


Replies are listed 'Best First'.
Re^4: Mulitple values for a key in a hash
by wishartz (Beadle) on Nov 30, 2007 at 13:54 UTC
    I'm sorry if this is a duplicate post, but I think I put my reply in the wrong place. At the momment I am using the following: But it is still reporting an error, even though, I am running it on the correct host and the file system is on the host. Is the check correct? Am I defreferencing it properly? The first if condition should have been false, but instead it is coming out as true and printing out error.
    $host=`hostname`; $filesystem = ARGV[0]; my %filesystems = ( hosta => (desc => 'x', filesystems => (qq|/dat +a/file/a| =>0, qq|/data/file/b| =>0)), tx05 => (desc => 'r',filesystems => (qq|/data/ +file/c| =>0, qq|no filesystemB for 2nd host | => 0))); if ( ! exists $filesystems{$host}{filesystems}{$filesystem} ) { print "error\n";} else {print "Found filesystem $filesystem on host $host\n". $filesyste +ms{$host}{descr}."\n";}
Re^4: Mulitple values for a key in a hash
by wishartz (Beadle) on Dec 03, 2007 at 09:42 UTC
    Dear Monks, I've tried making the hashes in the way that has been suggested to me, but the check does not work. It will always print out the message 'error', regardless of which host I am on and which file system I have chosen. So, the check always comes up as true, even when it should be false. I think the if condition just gives a reference and not the value. Can anybody help me with this? Ive hit a dead end, with it. Would I be better of making a nested foreach loop, with the outer hash key and the inner hash key and making a check that way?