Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I need to get the smallest number out of this array, I am having a little problem with that, any help?
'add_loc' => { '4' => 'Store 1|100 Main Street|55.06', '3' => 'Store 2|210 East Main Street|0.00', '2' => 'Store 3|301 West Main Street|11.23' },
I need to get the "0.00" out of this list.
Thanks!

Replies are listed 'Best First'.
Re: Get the Smallest Number
by Bloodnok (Vicar) on Aug 11, 2009 at 12:40 UTC
    Hmmm, am I the only one for whom the homework detector has just triggered ?

    The usual follow up question - What have you done/tried and better still, can you demonstrate it i.e. show us ? - naturally raises its head ...

    A user level that continues to overstate my experience :-))
Re: Get the Smallest Number
by si_lence (Deacon) on Aug 11, 2009 at 12:36 UTC
    I don't see an array in your data structure at all. It looks more like part of a 'Hash of Hashes'.
    You first need access the data you are interested in (maybe using two nested foreach keys) and then split the string by |, capturing the number of interest and find the min.
    If you show the code you have so far it would be much easier to provide you with more specific help.
    cheers, si_lence
      Why even if you have these two values at the end the IF statement still does work?
      if( 55.05 <= 0.00){ print "OK\n"; }else{ print "No\n"; }
Re: Get the Smallest Number
by toolic (Bishop) on Aug 11, 2009 at 13:09 UTC
Re: Get the Smallest Number
by bichonfrise74 (Vicar) on Aug 11, 2009 at 16:37 UTC
    Something like this?
    #!/usr/bin/perl use strict; use List::Util qw( min ); my @numbers; my %rec = ( 'add_loc' => { '4' => 'Store 1|100 Main Street|55.06', '3' => 'Store 2|210 East Main Street|0.00', '2' => 'Store 3|301 West Main Street|11.23' } ); for my $i (keys %rec) { for my $j ( values %{ $rec{$i} } ) { push( @numbers, (split( /\|/, $j))[-1] ); } } print min( @numbers );
Re: Get the Smallest Number
by Marshall (Canon) on Aug 11, 2009 at 16:49 UTC
    Your problem statement doesn't compile, so I just wrote some code assuming that 'add_loc' was a pointer to hash.

    I'm not sure what you desire. The code below allows for the possibility that multiple lines could have this same "min number at the end of the line, in that case, below code prints them all.

    #!/usr/bin/perl -w use strict; my $add_loc = { '4' => 'Store 1|100 Main Street|55.06', '3' => 'Store 2|210 East Main Street|0.00', '2' => 'Store 3|301 West Main Street|11.23', '6' => 'Store 5|501 SW Main Street|0.00', '8' => 'Store 8|999 Some Street|0.00', }; my $max_num = (~0); foreach my $min_key (sort by_last_hash_value (keys %$add_loc)) { my $last_num_in_string = (split(/[|]+/,$add_loc->{$min_key}) )[-1]; last if ( $last_num_in_string > $max_num); print "min string is: $add_loc->{$min_key}\n"; print " last value in that string is: ", $last_num_in_string, "\n"; $max_num = $last_num_in_string; } sub by_last_hash_value { my $a_num = (split(/|/,$add_loc->{$a}))[-1]; my $b_num = (split(/|/,$add_loc->{$b}))[-1]; $a_num <=> $b_num } __END__ min string is: Store 8|999 Some Street|0.00 last value in that string is: 0.00 min string is: Store 5|501 SW Main Street|0.00 last value in that string is: 0.00 min string is: Store 2|210 East Main Street|0.00 last value in that string is: 0.00
    Update: This ~0 statement was just a way to generate a huge number (one's complement of 0). From what I see, any huge number would do. I was surprised that this wasn't -1. For this code it didn't make any difference.
Re: Get the Smallest Number
by ikegami (Patriarch) on Aug 12, 2009 at 15:12 UTC
    use List::Util qw( min ); print min map { (split /\|/)[2] } values( %{ $h{add_loc} } );