Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Q: detect non-existent hash entries w/function?

Reply
Thread Tools

Q: detect non-existent hash entries w/function?

 
 
Daniel Friedman
Guest
Posts: n/a
 
      07-01-2003
I'm trying to figure out how to to detect non-existent hash entries
via a function, something of this general nature:


function hashexists($$) {
# first arg might have to be non-scalar to get this to work,
# that would be OK, too
my var1 = $_[0]; # maybe a scalar, maybe not
my $err_hint = $_[1];

if (exists <something here related to var1>) {
# If I can get this function to work, maybe I'll redefine
# it to provide the value of the specified hash,
# or die otherwise; for now, let's just...
return(1);
} else {
die "<something [else?] related to var1> doesn't exist; (hint:
$err_hint)"
return(0);
}


I haven't been able to do this sort of thing
with "use strict", which I regard as a must;
Any thoughts on how to achieve this?

Thanks very much,
--daniel
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      07-01-2003
Daniel Friedman <(E-Mail Removed)> wrote:

> my var1 = $_[0]; # maybe a scalar, maybe not



No "maybe not" about it.

If it starts with a dollar sign, it *is* a scalar.

So what did you really mean there?


> I haven't been able to do this sort of thing



I can't figure out what "sort of thing" you are after. Sorry.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
Martien Verbruggen
Guest
Posts: n/a
 
      07-01-2003
On 30 Jun 2003 18:03:42 -0700,
Daniel Friedman <(E-Mail Removed)> wrote:
> I'm trying to figure out how to to detect non-existent hash entries
> via a function, something of this general nature:


$ perldoc -f exists

> function hashexists($$) {
> # first arg might have to be non-scalar to get this to work,
> # that would be OK, too


The first argument will be a scalar. That's what that prototype does
for you. The first and second argument will be forced to be
interpreted in scalar context.

So,

sub foo($$) { print "$_[0]:$_[1]\n" }

my @a = (1, 2);
my @b = (1, 2, 3, 4);

foo(@a, @b);

Will print

2:4

> my var1 = $_[0]; # maybe a scalar, maybe not


No. A syntax error.

If you meant to write

my $var1 = $_[0];

then yes, it is a scalar. Always.

> my $err_hint = $_[1];
>
> if (exists <something here related to var1>) {


What does "something here related to var1" mean?

> # If I can get this function to work, maybe I'll redefine
> # it to provide the value of the specified hash,
> # or die otherwise; for now, let's just...
> return(1);
> } else {
> die "<something [else?] related to var1> doesn't exist; (hint:
> $err_hint)"


Similar question about this bit.

> return(0);


This return will never be reached. You just called die.

> }


I have tried, but I don't really know how to interpret this.

> I haven't been able to do this sort of thing
> with "use strict", which I regard as a must;


Well... I don't think strict has anything to do with it.

> Any thoughts on how to achieve this?


What is 'this'? Could you be a bit more specific? I am really not sure
what you're trying to do.

Martien
--
|
Martien Verbruggen | We are born naked, wet and hungry. Then
Trading Post Australia | things get worse.
|
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      07-01-2003
(E-Mail Removed) (Tad McClellan) writes:

> Daniel Friedman <(E-Mail Removed)> wrote:
>
> > my var1 = $_[0]; # maybe a scalar, maybe not

>
>
> No "maybe not" about it.
>
> If it starts with a dollar sign, it *is* a scalar.


Yes, this is so.

This is why I find the following annoying

my $glob;
my $r = \$glob;
print ref $r; # prints SCALAR
$glob = *FOO;
print ref $r; # prints GLOB

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
Reply With Quote
 
Charlton Wilbur
Guest
Posts: n/a
 
      07-01-2003
(E-Mail Removed) (Daniel Friedman) writes:

> I'm trying to figure out how to to detect non-existent hash entries
> via a function, something of this general nature:


If I understand what you're trying to do, why not just use

exists $hash{$key}

instead of creating a messy, complicated function to do the same thing?

Or, if you wanted to check multiple keys simply,

@nonexistent = grep { not exists $hash{$_} } @possible_keys;

Charlton

 
Reply With Quote
 
Daniel Friedman
Guest
Posts: n/a
 
      07-01-2003
There was apparently some ambiguity in my earlier post, so let me try
to clarify.

1) Purpose/motivation: I'm writing a script which will have lots of
places in which I want to carefully check for the existence of a hash
entry, and also avoid autovivification (I admit that the
non-autovification piece was not in my original post, I omitted it to
try to focus my question), before getting the value from the
hash--here's what I mean:

$errhint = __FILE__; # this is just an example
unless (exists $myhash{$foo}) {
die "$errhint: myhash{$foo} does not exist";
}
unless (exists $myhash{$foo}{$bar}) {
die "$errhint: myhash{$foo}{$bar} does not exist";
}
unless (exists $myhash{$foo}{$bar}{$baz}) {
die "$errhint: myhash{$foo}{$bar}{$baz} does not exist";
}
# apparently it exists, so let's get the value:
$thevalue = $myhash{$foo}{$bar}{$baz}

---> I want to make a function which does this. The function's
arguments
should be the "specification" of a hash whose existence is to be
checked, and some string for helping find the error ($errhint, above).
(The function would preferably be able to handle a non-fixed number
of hash levels.)

2) When I wrote in my earlier post "maybe a scalar, maybe not", I
meant that I wasn't fussy if the function receives the specification
as a single scalar argument (e.g. "myhash{$foo}{$bar}{$baz}") or in
some other form ("myhash", "$foo", "$bar", "$baz"). I think it can't
be a reference since I don't imagine it's possible to make a reference
to something which doesn't necessarily exist.

I hope that clarifies my question. Again, thanks very much,
--daniel
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      07-01-2003
>>>>> "DF" == Daniel Friedman <(E-Mail Removed)> writes:

DF> There was apparently some ambiguity in my earlier post, so let me try
DF> to clarify.

DF> 1) Purpose/motivation: I'm writing a script which will have lots of
DF> places in which I want to carefully check for the existence of a hash
DF> entry, and also avoid autovivification (I admit that the
DF> non-autovification piece was not in my original post, I omitted it to
DF> try to focus my question), before getting the value from the
DF> hash--here's what I mean:

first level hash lookups never autovivify. only multilevel lookups do
that. basically autoviv happens when you dereference through undef. see
my paper on this at:

http://tlc.perlarchive.com/articles/perl/ug0002.shtml

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      07-01-2003
Daniel Friedman <(E-Mail Removed)> wrote:

> There was apparently some ambiguity in my earlier post, so let me try
> to clarify.
>
> 1) Purpose/motivation: I'm writing a script which will have lots of
> places in which I want to carefully check for the existence of a hash
> entry, and also avoid autovivification (I admit that the
> non-autovification piece was not in my original post, I omitted it to
> try to focus my question),




Errr, but that *is* your question:

detect non-existent hash entries w/o autoviv'ing

The answer to your OP questions was:

exists $myhash{$foo}{$bar}{$baz}

which doesn't avoid the autoviving for you.


> ---> I want to make a function which does this. The function's
> arguments
> should be the "specification" of a hash whose existence is to be
> checked, and some string for helping find the error ($errhint, above).
> (The function would preferably be able to handle a non-fixed number
> of hash levels.)
>
> 2) When I wrote in my earlier post "maybe a scalar, maybe not", I
> meant that I wasn't fussy if the function receives the specification
> as a single scalar argument (e.g. "myhash{$foo}{$bar}{$baz}") or in
> some other form ("myhash", "$foo", "$bar", "$baz").


> I hope that clarifies my question.



It doesn't clarify your question, it states your question.

Here is a function to get you started, it wants a hash-ref as
the first arg, followed by a list of keys, eg:

do_something() if exists_no_viv(\%myhash, $foo, $bar, $baz)


---------------------------
sub exists_no_viv { # "walk" down a chain of hashrefs
my $h = shift; # a ref to hash, start at the "top"

while ( my $k = shift ) {
return 0 unless exists $h->{$k}; # fail w/o autoviv
if ( ref $h->{$k} eq 'HASH' ) # go one level "deeper"
{ $h = $h->{$k} }
}
return 1;
}
---------------------------


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Daniel Friedman
Guest
Posts: n/a
 
      07-02-2003
Tad,

Late yesterday, the same idea sprouted in my mind, and lo, you've
given me the code! Bingo, this is just what I was looking for; thanks
very much.

--dan


(E-Mail Removed) (Tad McClellan) wrote in message news:<(E-Mail Removed)>.. .

> Here is a function to get you started, it wants a hash-ref as
> the first arg, followed by a list of keys, eg:
>
> do_something() if exists_no_viv(\%myhash, $foo, $bar, $baz)
>
>
> ---------------------------
> sub exists_no_viv { # "walk" down a chain of hashrefs
> my $h = shift; # a ref to hash, start at the "top"
>
> while ( my $k = shift ) {
> return 0 unless exists $h->{$k}; # fail w/o autoviv
> if ( ref $h->{$k} eq 'HASH' ) # go one level "deeper"
> { $h = $h->{$k} }
> }
> return 1;
> }
> ---------------------------

 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      07-02-2003
>>>>> "DF" == Daniel Friedman <(E-Mail Removed)> writes:

<don't top post>

DF> Late yesterday, the same idea sprouted in my mind, and lo, you've
DF> given me the code! Bingo, this is just what I was looking for;
DF> thanks very much.

again, i will point you to this article which has similar code.

http://tlc.perlarchive.com/articles/perl/ug0002.shtml

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Picking X random entries from linked list of Y entries Don Bruder C Programming 3 08-03-2010 09:10 AM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Tying up Port Login table entries with Port Table Entries in CISCO SNMP John Ramsden Cisco 0 07-24-2004 04:03 PM
Sorting entries in a DBM hash by one of the variables Topher Perl 1 01-21-2004 02:28 PM



Advertisments