I will try to explain what is my purpose:
I have a script that parsing the ARGV argumemts: tvc1=value
tvc2=value...tvc5=value or tvc=value (which sets all tvc1..tvc5 to specific
value).
Now, I don't know if the arguments exists in the command line of not...since it
an optional arguments.
So....
I can do 2 things:
1) my $found = 1;
Foreach (keys %tvc))
{
$found = 0;
}
"Do something" if ($found);
2) "Do something" if (%tvc);
My question is should option 2 will be OK ? I thought it might send a warnings
but it did when I tried to do: "Do something" if (defined(%tvc));
which is I believe to be a mistake....since defined doesn't loop on hash keys -
which I thought at start
I found that option 2 is OK by me - no warnings :-)
Thanks
Chanan
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of
Avishalom Shalit
Sent: Thursday, January 22, 2009 4:02 PM
To: Perl in Israel
Subject: Re: [Israel.pm] FW: FW: check if hash is defined - without the keys
i really really recommend the perldoc pages.
"perldoc perltoc" may be a good place to start
i think that you would really find it useful to study the "context"
aspect of perl
try answering these
1) what do you get when you evaluate a list in hash context ? (how
about the other way around?)
2) what do you get when you evaluate a hash in scalar context?
---
as for your first question ,
i am not sure why you don't like the idea of looping or why a warning
is better than an explicit check .
---
perldoc -f grep
perldoc -f keys
2009/1/22 bc.other <[email protected]>:
> OK, found that I can use the if (%tvc or %tvw) with use strict / warnings it
> works (no need for defined function).
> I understood it returns a value ? but can anyone tell me what this value
> represent ?
>
> I tried this code:
> my %tvc = (1 10);
> my $sc = %tvc;
>
> print $sc; --> printing 1/8 (does it means 1 key, 8 bytes) ?
> Thanks
> Chanan
>
> -----Original Message-----
> From: [email protected] [mailto:[email protected]] On Behalf
> Of Chanan Berler
> Sent: Thursday, January 22, 2009 3:31 PM
> To: 'Perl in Israel'
> Subject: [Israel.pm] FW: check if hash is defined - without the keys
>
>
> Hi All,
>
> I got a hash with key1…key25, I am looking to know if the keys are defined
> or not.
> But I don't like the way like: if defined($hash{key1}) or …..
> defined($hash{key5})
>
> I also don't like much the idea of looping over the keys…
> Q: is there a way to check if hash was defined or not….using the defined
> function showed a warning…
>
> PS: I can stop using the use strict / warnings – and try this if (%tvc or
> %tvw) – and it works…
> But I still want to use them…
>
> Thanks
> Chanan
>
> _______________________________________________
> Perl mailing list
> [email protected]
> http://perl.org.il/mailman/listinfo/perl
>
> _______________________________________________
> Perl mailing list
> [email protected]
> http://perl.org.il/mailman/listinfo/perl
>
--
-- vish
_______________________________________________
Perl mailing list
[email protected]
http://perl.org.il/mailman/listinfo/perl
_______________________________________________
Perl mailing list
[email protected]
http://perl.org.il/mailman/listinfo/perl