Re: References/unitialized variable error message
In article <[EMAIL PROTECTED]> wrote "Geoffrey F. Green" <[EMAIL PROTECTED]>: > #!/usr/bin/perl -w > > my %table = ( > "key1" => > {search=> "alpha", > shortname=> "beta", > }, > "key2" => > {search => "gamma", > shortname=> "delta", > }, > "key3" => > {search=> "epsilon", > shortname=> "whatever", > }, >); I'd like to give another hint: I find it dangerous, to repeat in every line search and shortname. The chance of mistyping is really big. Even if you use Copy+Paste (what's never a good solution) you get the trouble when changing later. Again, I would be too lazy - even for writing and for reading :-) Here's one possibility: my %table = map { my ($key, $search, $short) = split / /, $_; ($key => { search=> $search, shortname => $shortname } ) } split /\n/, <<'TABLE'; key1 alpha beta key2 gamma delta key3 epsilon whatever TABLE That of course only goes strait when the keys don't have white spaces. And again, it only makes sense when you will use more than 3 keys a day. Greetings, Andrea -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: References/unitialized variable error message
On 2/16/02 7:15 PM, "Briac Pilpré" <[EMAIL PROTECTED]> wrote: > To iterate over a hash, you want to use the keys() function. I once knew that, but I haven't used perl enough so I forgot. I gues that's why they call this list "Beginners." Thanks a lot - geoff -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: References/unitialized variable error message
On Sat, Feb 16, 2002 at 07:07:28PM -0500, Geoffrey F. Green wrote: > my %table = ( > "key1" => > {search=> "alpha", > shortname=> "beta", > }, > "key2" => > {search => "gamma", > shortname=> "delta", > }, > "key3" => > {search=> "epsilon", > shortname=> "whatever", > }, > ); > > foreach my $keys (%table) { You say $keys here, and treat $keys as a key, but when you iterate over %table you're iterating over the list ('key1', $hashref1, 'key2', $hashref2, 'key3', $hashref3), i.e. both the keys -and- the values. You should be using keys(%table). foreach my $keys (keys %table) { ... } Also, I'd suggest naming it $key, not $keys; you're accessing one key at a time, not multiple keys. > print $table{$keys}->{search},"\n"; > print $table{$keys}->{shortname},"\n"; > }; > Camel's description of the error, and undefined variables, didn't clarify > matters for me, and unless I missed something fundamental, the FAQ didn't > have an answer to this conundrum. I'm not sure why you didn't understand the description, possibly because you didn't expect the error from what you were using. The problem occurred because you were trying to access $table{$hashref1}->{search}. There was no such key, so you got undef. > I also can't remember, off the top of my head, the sixth letter of the > Greek alphabet, but that's not important right now. zeta > 2. I also don't understand the extra newline I get between each pass of the > foreach loop. With warnings disabled, the output is as follows: Just because one of the values in your print statement is undefined doesn't mean the entire print statement is ignored. You have: print $table{$keys}->{search}, "\n"; If $table{$keys}->{search} is undefined, it's stringified to "" and a warning is issued. So, in effect, you have: print "", "\n"; Which prints just a newline, thus the "extra" whitespace. Michael -- Administrator www.shoebox.net Programmer, System Administrator www.gallanttech.com -- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: References/unitialized variable error message
On Sun, 17 Feb 2002 at 00:07 GMT, Geoffrey F. Green wrote: > Hi. Newbie to perl here. I've got two problems (well, two perl-related > problems), relating to the same program. To iterate over a hash, you want to use the keys() function. foreach my $keys ( keys %table) { print $table{$keys}->{search},"\n"; print $table{$keys}->{shortname},"\n"; } Otherwise, perl iterates alternatively over the keys and the values of the hash, which causes the extra newlines and the warnings. -- briac << dynamic .sig on strike, we apologize for the inconvenience >> -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
References/unitialized variable error message
Hi. Newbie to perl here. I've got two problems (well, two perl-related problems), relating to the same program. 1. I'm putting together some test programs to try to get down the syntax for references, etc., but I'm coming across a weird situation I can't figure out. I'm setting up a hash of hashes. For some reason, I'm being told that a particular variable that I want printed is uninitialized -- but then perl goes ahead and prints the variable. Here's the test program, named "referencespractice.pl": #!/usr/bin/perl -w my %table = ( "key1" => {search=> "alpha", shortname=> "beta", }, "key2" => {search => "gamma", shortname=> "delta", }, "key3" => {search=> "epsilon", shortname=> "whatever", }, ); foreach my $keys (%table) { print $table{$keys}->{search},"\n"; print $table{$keys}->{shortname},"\n"; }; I expect this output: alpha beta gamma delta epsilon whatever Instead, I get this: alpha beta Use of uninitialized value in print at ./referencespractice.pl line 20. Use of uninitialized value in print at ./referencespractice.pl line 21. gamma delta Use of uninitialized value in print at ./referencespractice.pl line 20. Use of uninitialized value in print at ./referencespractice.pl line 21. epsilon whatever Use of uninitialized value in print at ./referencespractice.pl line 20. Use of uninitialized value in print at ./referencespractice.pl line 21. (Needless to say, the errors occur within the two lines in the foreach loop.) Camel's description of the error, and undefined variables, didn't clarify matters for me, and unless I missed something fundamental, the FAQ didn't have an answer to this conundrum. I also can't remember, off the top of my head, the sixth letter of the Greek alphabet, but that's not important right now. 2. I also don't understand the extra newline I get between each pass of the foreach loop. With warnings disabled, the output is as follows: alpha beta gamma delta epsilon whatever Why are there extra newlines? Thanks in advance. - geoff -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]