Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Hash with keys only

Tags:

hash

perl

Can only keys be pushed in PERL hash, I mean, Can hash be created without corresponding values?

I want to create a hash which behave just like array i.e. hash with only keys but not corresponding values. Example is given below:

my %feedHash;
while(<CFG>)
{
    chomp($_);
    my @val=split(/:/,$_);
    chomp($val[0]);
    my $feedId=$val[0];
    if(!exists $feedHash{$feedId})
    {
      print "\n$feedId Feed is not present";
      $hash{$feedId} = undef;
      mkdir "LoadReports/$feedId" or die $!;                    
    }
    else
    {
       print '\nFeed is already present';
    }
}

It is giving message: "Feed is not present" even for same feed id second time in loop

like image 662
user3732491 Avatar asked Jan 10 '23 10:01

user3732491


1 Answers

You can make hash with your keys, where values are set to undef

my %hash;
@hash{qw(key1 key2 key3)} = ();

to check/set for particular hash key,

if (!exists $hash{key4}) { $hash{key4} = undef } 
like image 166
mpapec Avatar answered Jan 22 '23 17:01

mpapec