I hate having to say the exists argument twice

J

jidanni

I hate having to say things twice,
print $h{$key}{$_} if exists $h{$key}{$_};
But I can't do
print if exists $_ for $h{$key}{$_};
Bzzt: exists argument is not a HASH or ARRAY element.
 
D

Dr.Ruud

I hate having to say things twice,
print $h{$key}{$_} if exists $h{$key}{$_};
But I can't do
print if exists $_ for $h{$key}{$_};
Bzzt: exists argument is not a HASH or ARRAY element.

print for exists_or $h{$key}, $_;

;)
 
S

smallpond

I hate having to say things twice,
print $h{$key}{$_} if exists $h{$key}{$_};
But I can't do
print if exists $_ for $h{$key}{$_};
Bzzt: exists argument is not a HASH or ARRAY element.

Is it a case where you can use defined instead of exists?

print $h{$key}{$_} // "";
 
J

jidanni

I'm sorry I don't understand smallpond and Dr. Rudd's answers.
Is there a solution that won't make a warning when -w is in use and the
item doesn't exist or is defined I suppose?
 
S

sln

I hate having to say things twice,
print $h{$key}{$_} if exists $h{$key}{$_};
But I can't do
print if exists $_ for $h{$key}{$_};
Bzzt: exists argument is not a HASH or ARRAY element.

I didn't see the Bzzt exists conditional.
What happens she $h exists?

-sln
 
J

jidanni

I didn't see the Bzzt exists conditional.
OK, I don't want to repeat $m[$_] (or something much longer actually),
$ perl -wle '@m=(1,2); for(0..3){print $m[$_] if exists $m[$_]}'
1
2
Nor do I want to use a temporary variable. But I can't just do
$ perl -wle '@m=(1,2); for(0..3){for($m[$_]){print $_ if exists $_}}'
exists argument is not a HASH or ARRAY element at -e line 1.
 
U

Uri Guttman

j> Nor do I want to use a temporary variable. But I can't just do
j> $ perl -wle '@m=(1,2); for(0..3){for($m[$_]){print $_ if exists $_}}'
j> exists argument is not a HASH or ARRAY element at -e line 1.

you at least need to repeat the hash if you want that style of code. if
the hash is deeper than one level you can take a ref to the desired
level and simplify that way (and speed it up).

but i will ask if this is an xy problem. exists is a useful but in
general rarely needed function. if you are stuck on calling it maybe you
have a design that can be improved so you don't need exists. checking
with exists and then accessing the element is also not common IMO. maybe
you only need defined or boolean? or you want to initialize an element
if it wasn't already (//= should do that fine).

so what is the bigger picture that you are addressing and don't mention
exists in your description. exists is a solution to a small specific
problem and not a problem description.

uri
 
S

smallpond

I'm sorry I don't understand smallpond and Dr. Rudd's answers.
Is there a solution that won't make a warning when -w is in use and the
item doesn't exist or is defined I suppose?

perl 5.10 has this new operator. $a // $b is short for:
defined($a) ? $a : $b

It would be nice if there was a similar op for exists, but
for many uses of hashes a value is always defined when the
key exists. It depends on your program.
 
U

Uri Guttman

s> perl 5.10 has this new operator. $a // $b is short for:
s> defined($a) ? $a : $b

s> It would be nice if there was a similar op for exists, but
s> for many uses of hashes a value is always defined when the
s> key exists. It depends on your program.

from a point of view of a hash element having a value, //= is good
enough. in many cases ||= is also good enough if you never allow 0 or ''
as a legal value. and autovivication handles all cases where you want a
ref in the slot and you never need to deal with exists. as i posted
before, exists is a useful but rarely needed function. we never got
(even though i asked) why the OP must use exists.

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,213
Messages
2,571,108
Members
47,700
Latest member
Naveed baloch

Latest Threads

Top