Professional Web Applications Themes

How to find if a key exist in hash? - PERL Beginners

Hi, I have the following code, and I know it is HORRIBLE. I wonder if I can do it in more efficient and elegant way? Thanks so much and Regards, Edward WIJAYA SINGAPORE __BEGIN__ use strict; use warnings; use Getopt::Std; use Data::Dumper; my %hash = ( A => 'blabla', B => 'dadada', C => 'tititi', ); my $s = 'A'; my $get = check_ifHash_key_exist(\%hash, $s); print "Got it: $get\n"; #--- this is how I do it (don't laugh) -------------- sub check_ifHash_key_exist { my ($hash, $str) = _; my $found_str; my array = keys %{$hash}; for (my $i= 0; $i < ...

  1. #1

    Default How to find if a key exist in hash?

    Hi,

    I have the following code,
    and I know it is HORRIBLE.

    I wonder if I can do it in more
    efficient and elegant way?

    Thanks so much
    and

    Regards,
    Edward WIJAYA
    SINGAPORE


    __BEGIN__
    use strict;
    use warnings;
    use Getopt::Std;
    use Data::Dumper;

    my %hash = (
    A => 'blabla',
    B => 'dadada',
    C => 'tititi',
    );

    my $s = 'A';
    my $get = check_ifHash_key_exist(\%hash, $s);
    print "Got it: $get\n";

    #--- this is how I do it (don't laugh) --------------

    sub check_ifHash_key_exist {

    my ($hash, $str) = _;
    my $found_str;
    my array = keys %{$hash};
    for (my $i= 0; $i < keys %{$hash}; $i++) {
    if ($array[$i] eq $str) {
    $found_str = $array[$i];
    last;
    }
    }
    return $found_str;
    }
    __END__
    Edward Guest

  2. #2

    Default Re: How to find if a key exist in hash?

    Edward Wijaya wrote: 

    There is a builtin Perl function for the purpose.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Guest

  3. #3

    Default RE: How to find if a key exist in hash?


     


    perldoc -f exists

    See Also:
    http://search.cpan.org/~bpowers/Hash-NoVivify-0.01/NoVivify.pm

    HTH,

    Josť.


    **** DISCLAIMER ****

    "This e-mail and any attachment thereto may contain information which is confidential and/or protected by intellectual property rights and are intended for the sole use of the recipient(s) named above.
    Any use of the information contained herein (including, but not limited to,total or partial reproduction, communication or distribution in any form) by other persons than the designated recipient(s) is prohibited.
    If you have received this e-mail in error, please notify the sender either by telephone or by e-mail and delete the material from any computer".

    Thank you for your cooperation.

    For further information about Proximus mobile phone services please see ourwebsite at http://www.proximus.be or refer to any Proximus agent.

    NYIMI Guest

  4. #4

    Default Re: How to find if a key exist in hash?


    %a = (
    "a" => 1,
    "b" => 2,
    "c" => 3
    );

    $searchKey = "a";

    print "Found $searchKey" if defined($a{$searchKey});

    -------------------------------------
    This mail is from: <org>
    -------------------------------------

    max4o@magic.g00net.org Guest

  5. #5

    Default Re: How to find if a key exist in hash?

    g00net.org wrote: 

    Try that with this hash:

    my %a = (
    a => undef,
    b => undef,
    c => undef,
    );

    defined() does not tell you if a key exists.

    perldoc -f exists


    John
    --
    use Perl;
    program
    fulfillment
    John Guest

Similar Threads

  1. FTP with File::Find-like Capabilities: Does It Exist?
    By Jim Keenan in forum PERL Modules
    Replies: 4
    Last Post: April 27th, 01:29 PM
  2. ntpdate - can't find a server that does exist.
    By Dr. David Kirkby in forum AIX
    Replies: 1
    Last Post: August 7th, 12:29 PM
  3. Find data not exist in tableA, but in tableB
    By Mindy in forum Microsoft SQL / MS SQL Server
    Replies: 2
    Last Post: July 30th, 08:46 PM
  4. Replies: 0
    Last Post: July 20th, 02:51 AM
  5. how to find out if control exist
    By Ante Perkovic in forum ASP.NET General
    Replies: 1
    Last Post: June 25th, 03:30 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139