declaring a zero size hash
I'm trying to declare a zero size hash so a sub function can populate it and be see by all other sub's. my %loginHash(); But the use strict doesn't like it. All examples of making a hash structure is hard coded in the program or is made reading from a file. When I try to run the script all I get is syntax error near %loginHash( What am I missing? Don Dukelow smime.p7s Description: S/MIME cryptographic signature
Re: declaring a zero size hash
Just write it like: my %loginHash = (); This should work. -Original Message- From: Dukelow, Don [EMAIL PROTECTED] Sent: Dec 13, 2006 12:23 AM To: beginners@perl.org Subject: declaring a zero size hash I'm trying to declare a zero size hash so a sub function can populate it and be see by all other sub's. my %loginHash(); -- Books below translated by me to Chinese. Practical mod_perl: http://home.earthlink.net/~pangj/mod_perl/ Squid the Definitive Guide: http://home.earthlink.net/~pangj/squid/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: declaring a zero size hash
On 12/12/06, Dukelow, Don [EMAIL PROTECTED] wrote: I'm trying to declare a zero size hash so a sub function can populate it and be see by all other sub's. my %loginHash(); my %loginHash; should be enough. But the use strict doesn't like it. It is not use strict that does not like it. It is Perl itself -- this is a syntax error: $ perl -e 'my %h();' syntax error at -e line 1, near %h( Execution of -e aborted due to compilation errors. All examples of making a hash structure is hard coded in the program or is made reading from a file. When I try to run the script all I get is syntax error near %loginHash( What am I missing? Something like this might do what you want: # read a file and store each line at a bucket of a hash sub pop_hash { my $h = shift; # the hash ref my $f = shift; # the filehandle while ($f) { $h-{$.} = $_; } return $h; # but it was already changed in-place } my %h; pop_hash(\%h, *STDOUT); use Data::Dumper; print Dumper(\%h); -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: declaring a zero size hash
On 12/12/06, Dukelow, Don [EMAIL PROTECTED] wrote: I'm trying to declare a zero size hash so a sub function can populate it and be see by all other sub's. my %loginHash(); Maybe you mean this? my %loginHash = (); But every new variable (which is what 'my' is declaring) starts out empty, so all you really need is this: my %loginHash; Hope this helps! --Tom Phoenix Stonehenge Perl Training -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: declaring a zero size hash
I think you need to do: my %loginhash = {}; Kim Helliwell LSI Logic Corporation Work: 408 433 8475 Cell: 408 832 5365 [EMAIL PROTECTED] Please Note: My email address changed to [EMAIL PROTECTED] on Oct 14. The old email address ([EMAIL PROTECTED]) will stop working after Jan 15, 2007. Please update your address book and distribution lists accordingly. Thank you. -Original Message- From: Dukelow, Don [mailto:[EMAIL PROTECTED] Sent: Tuesday, December 12, 2006 8:23 AM To: beginners@perl.org Subject: declaring a zero size hash I'm trying to declare a zero size hash so a sub function can populate it and be see by all other sub's. my %loginHash(); But the use strict doesn't like it. All examples of making a hash structure is hard coded in the program or is made reading from a file. When I try to run the script all I get is syntax error near %loginHash( What am I missing? Don Dukelow -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: declaring a zero size hash
On 12/12/06, Helliwell, Kim [EMAIL PROTECTED] wrote: I think you need to do: my %loginhash = {}; That's not right. {} is a hash ref, not a hash. It stands for a scalar value. When you do that my %h = {} or, for the same result, my %h = 1; my %h = abacate; you end with a hash with one pair, whose key is the given scalar and the value is undef. See the output of $ perl -MData::Dumper -e '%h = {}; print Dumper(\%h)' $VAR1 = { 'HASH(0x10240170)' = undef }; Kim Helliwell LSI Logic Corporation Work: 408 433 8475 Cell: 408 832 5365 [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response