Re: Warnings when sorting by hashref

2017-04-11 Thread Chris Fedde
I like to define a value subroutine.

sub myvalue {
   return uc($options{$_[0]}->{type} // "")
}

This particular one returns the empty string ("") if
$options{$_[0]}->{type} is undefined.
Now the sort becomes:

sort {myvalue($a) cmp myvalue($b)} keys %options

This code is untested but maybe you see the idea.

chris

On Tue, Apr 11, 2017 at 7:13 AM, Mike Martin  wrote:

> Hi
>
> I have the following code as an example against a hash of hashes, to sort
> by hashrf key
>
> foreach my $opt (sort {uc($options{$b}->{type}) cmp
> uc($options{$a}->{type})} keys %options){
> my $type=$options{$opt}->{vtype};
> $video_type->append_text($type) if defined($type) ;
> }
>
> The sort function works perfectly, however perl throws up the following
> warning for each line
>
> Use of uninitialized value in uc at
>
> Any ideas how to get rid of the warning
>
> thanks
>
> Mike
>
>


Re: Warnings when sorting by hashref

2017-04-11 Thread Jim Gibson

> On Apr 11, 2017, at 6:13 AM, Mike Martin  wrote:
> 
> Hi
> 
> I have the following code as an example against a hash of hashes, to sort by 
> hashrf key
> 
> foreach my $opt (sort {uc($options{$b}->{type}) cmp uc($options{$a}->{type})} 
> keys %options){
>my $type=$options{$opt}->{vtype};
>$video_type->append_text($type) if defined($type) ;
>}
> 
> The sort function works perfectly, however perl throws up the following 
> warning for each line
> 
> Use of uninitialized value in uc at
> 
> Any ideas how to get rid of the warning

Make sure that all $options{$key}->{type} values are defined:

foreach my $key ( keys %options ) {
 $options{$key}->{type} = ‘’ unless defined $options{$key}->{type};
}

Substitute something else for ‘’ depending upon where you want to sort 
undefined {type} values in your sort order.


Jim Gibson

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Warnings when sorting by hashref

2017-04-11 Thread Mike Martin
Hi

I have the following code as an example against a hash of hashes, to sort
by hashrf key

foreach my $opt (sort {uc($options{$b}->{type}) cmp
uc($options{$a}->{type})} keys %options){
my $type=$options{$opt}->{vtype};
$video_type->append_text($type) if defined($type) ;
}

The sort function works perfectly, however perl throws up the following
warning for each line

Use of uninitialized value in uc at

Any ideas how to get rid of the warning

thanks

Mike