Re: Sort + Use of uninitialized value

2007-04-25 Thread Owen Cook
On Wed, Apr 25, 2007 at 01:37:24AM -0400, yitzle wrote:
 Warning message:
 Use of uninitialized value in numeric comparison (=) at ...
 
 Code:
 foreach (sort { $dHash{$b}{'VAL} = $dHash{$a}{'VAL'} } keys %dHash) {

perhaps

  foreach (sort { $dHash{$b}{'VAL'} = $dHash{$a}{'VAL'} } keys %dHash) {
 
 How do I fix? Should my sort function be checking for variable
 defined? What do I return on undefined?



Owen

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Sort + Use of uninitialized value

2007-04-25 Thread Chas Owens

On 4/25/07, yitzle [EMAIL PROTECTED] wrote:

Warning message:
Use of uninitialized value in numeric comparison (=) at ...

Code:
foreach (sort { $dHash{$b}{'VAL} = $dHash{$a}{'VAL'} } keys %dHash) {

How do I fix? Should my sort function be checking for variable
defined? What do I return on undefined?


It sounds like one of your keys does not have the key 'VAL' defined or
its value is undef.  This may or may not be an error (which is why it
is a warning message).  I would loop over %dHash until I found the bad
value(s) and then decided what to do from there.

for my $key (sort keys %dHash) {
   if (not exists $dHash{$key}{VAL}) {
   print $key does not have a VAL\n;
   elsif (not defined $dHash{$key}{VAL}) {
print $key's VAL is undefined
   }
}

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Sort + Use of uninitialized value

2007-04-25 Thread yitzle

Inside the loop I check if the value is defined, so I don't care where
in the order the undefined one shows up in. I don't want to delete
undefined ones or anything...


On 4/25/07, Chas Owens [EMAIL PROTECTED] wrote:

On 4/25/07, yitzle [EMAIL PROTECTED] wrote:
 Warning message:
 Use of uninitialized value in numeric comparison (=) at ...

 Code:
 foreach (sort { $dHash{$b}{'VAL} = $dHash{$a}{'VAL'} } keys %dHash) {

 How do I fix? Should my sort function be checking for variable
 defined? What do I return on undefined?

It sounds like one of your keys does not have the key 'VAL' defined or
its value is undef.  This may or may not be an error (which is why it
is a warning message).  I would loop over %dHash until I found the bad
value(s) and then decided what to do from there.

for my $key (sort keys %dHash) {
if (not exists $dHash{$key}{VAL}) {
print $key does not have a VAL\n;
elsif (not defined $dHash{$key}{VAL}) {
 print $key's VAL is undefined
}
}



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Sort + Use of uninitialized value

2007-04-25 Thread Tom Phoenix

On 4/24/07, yitzle [EMAIL PROTECTED] wrote:


Warning message:
Use of uninitialized value in numeric comparison (=) at ...

Code:
foreach (sort { $dHash{$b}{'VAL} = $dHash{$a}{'VAL'} } keys %dHash) {

How do I fix?


Give it an initialized value. Maybe like this?

 sort { ($dHash{$b}{VAL} || 0) = ($dHash{$a}{VAL} || 0) } keys %dHash

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/




Re: Sort + Use of uninitialized value

2007-04-25 Thread Chas Owens

On 4/25/07, yitzle [EMAIL PROTECTED] wrote:

Inside the loop I check if the value is defined, so I don't care where
in the order the undefined one shows up in. I don't want to delete
undefined ones or anything...


Then you can either turn off the warnings for that section (not
advised), ignore the warnings (also not advised), or give the null
values a value.  In the following example I use 0 as the value, but it
could be any numeric value depending on where you want undefined
values to be sorted.

#!/usr/bin/perl

use strict;
use warnings;

my %h = (
   a = { val = 3 },
   b = { val = 34 },
   c = { val = 4 },
   d = { }
);

for my $key (sort { ($h{$a}{val} || 0) = ($h{$b}{val} || 0) } keys %h) {
   print $key\n;
}

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Sort + Use of uninitialized value

2007-04-25 Thread Rob Dixon

yitzle wrote:


On 4/25/07, Chas Owens [EMAIL PROTECTED] wrote:


On 4/25/07, yitzle [EMAIL PROTECTED] wrote:


Warning message:
Use of uninitialized value in numeric comparison (=) at ...

Code:
foreach (sort { $dHash{$b}{'VAL} = $dHash{$a}{'VAL'} } keys %dHash) {

How do I fix? Should my sort function be checking for variable
defined? What do I return on undefined?


It sounds like one of your keys does not have the key 'VAL' defined or
its value is undef.  This may or may not be an error (which is why it
is a warning message).  I would loop over %dHash until I found the bad
value(s) and then decided what to do from there.

for my $key (sort keys %dHash) {
if (not exists $dHash{$key}{VAL}) {
print $key does not have a VAL\n;
elsif (not defined $dHash{$key}{VAL}) {
 print $key's VAL is undefined
}
}


Inside the loop I check if the value is defined, so I don't care where
in the order the undefined one shows up in. I don't want to delete
undefined ones or anything...


In that case just create a list of the keys with a defined VAL value
before you do the sort:

 my @keys = grep defined $dHash{$_}{VAL}, keys %dHash;

 foreach (sort { $dHash{$b}{VAL} = $dHash{$a}{VAL} } @keys) {
   print $_, \n;
 }

HTH,

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Sort + Use of uninitialized value

2007-04-25 Thread Rob Dixon

yitzle wrote:

Inside the loop I check if the value is defined, so I don't care where
in the order the undefined one shows up in. I don't want to delete
undefined ones or anything...


On 4/25/07, Chas Owens [EMAIL PROTECTED] wrote:

On 4/25/07, yitzle [EMAIL PROTECTED] wrote:
 Warning message:
 Use of uninitialized value in numeric comparison (=) at ...

 Code:
 foreach (sort { $dHash{$b}{'VAL} = $dHash{$a}{'VAL'} } keys %dHash) {

 How do I fix? Should my sort function be checking for variable
 defined? What do I return on undefined?

It sounds like one of your keys does not have the key 'VAL' defined or
its value is undef.  This may or may not be an error (which is why it
is a warning message).  I would loop over %dHash until I found the bad
value(s) and then decided what to do from there.

for my $key (sort keys %dHash) {
if (not exists $dHash{$key}{VAL}) {
print $key does not have a VAL\n;
elsif (not defined $dHash{$key}{VAL}) {
 print $key's VAL is undefined
}
}


... or you could define a sort subroutine and switch off warnings of undefined
values just within that routine:

 sub byVal {
   no warnings 'uninitialized';
   $dHash{$b}{VAL} = $dHash{$a}{VAL};
 } 


 foreach (sort byVal keys %dHash) {
   print $_, \n;
 }

HTH too!

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Sort + Use of uninitialized value

2007-04-25 Thread yitzle

In that case just create a list of the keys with a defined VAL value
before you do the sort:

  my @keys = grep defined $dHash{$_}{VAL}, keys %dHash;

  foreach (sort { $dHash{$b}{VAL} = $dHash{$a}{VAL} } @keys) {
print $_, \n;
  }

HTH,

Rob


This solution appeals to me. I'll use it. Thanks!

P.S. What's HTH?

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Sort + Use of uninitialized value

2007-04-25 Thread Rob Dixon

yitzle wrote:


P.S. What's HTH?


Hope This Helps :)

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/