in reply to Re^3: Need help figuring out how to order/eval the numbers
in thread Need help figuring out how to order/eval the numbers

I am now stopped with my new test to implement new learnings...I read and reviewed other code and still lost...maybe I need a break or think of a better test prog to try out skills

need help here with doing a proper sub routine check on the values of a hash ref. while using the <=>

IF the value = value1 then print out to file

IF value of either is not the same then print those to a file

1 = 1 2 = 2 3 = 3 4 = 4 5 = 5
1 = 2 2 = 2 3 = 4 4 = 7 5 = 5 6 = 6
use strict; use warnings; use autodie; use diagnostics; use Data::Dump qw(dump); my %seen; open my $in,'<','./Test_Data.txt'; while(<$in>){ my ($key,$value)= split /[\s=\s]+/; $seen{$key}[0]=$value; } close $in; #using ONE hashes to check key open my $in1,'<','./Test_Data1.txt'; while(<$in1>){ my ($key,$value)= split /[\s=\s]+/; $seen{$key}[1]=$value if (exists $seen{$key}); } close $in1; #dump \%seen; #section uses as follows: ? conditional to write on different output f +iles #also, 1st sub to written by me... to check the values open my $out,'>','./OUT_test_data_keys_and_sorted_values_match.txt'; open my $out1,'>','./OUT_test_data_No_values_matched.txt'; foreach my $key ( sort { check_values()} keys %seen){ if ($key){ my $fh = defined $seen{$key}[0] && defined $seen{$key}[1] ? $out:$out1; print $fh "$key => ",join(',',@{$seen{$key}}),"\n"; } } close $out; close $out1; #here is first SUBROUTINE checks that value [0] == value[1] #my %number; sub check_values{ for my $key (sort {$a<=>$b} keys %seen){ #don't know how ,where to get valueat [0],value at[1] from??wh +o has the values? what is really (a and b) in, is it $_ or %seen? wha +t am I really evaluating with,is it like this: $seen{$key}[0]=$a or $ +seen{$key}[1]=$b...completely lost... }

Replies are listed 'Best First'.
Re^5: Need help figuring out how to order/eval the numbers
by roboticus (Chancellor) on Jun 25, 2015 at 02:23 UTC

    perlnewby:

    The function you give sort has one job: to tell sort which order you want between two items: $a and $b. If you want the value in $a to sort after the value in $b, your function needs to return 1. If you want it to sort before $b, return -1.

    So you don't need to figure out how to get the values, they're handed to you as $a and $b. You simply need to tell sort how to decide which order you want for any two values.

    $ cat foo.pl use strict; use warnings; my @list = (qw(now is the time)); sub compare { my ($left, $right) = @_; print "Comparing $left and $right.\n"; return 1 if $left gt $right; return -1 if $left lt $right; return 0; } print "BEFORE: ", join(" ", @list), "!\n"; @list = sort { compare($a,$b) } @list; print "AFTER: ", join(" ", @list), "?\n"; $ perl foo.pl BEFORE: now is the time! Comparing now and is. Comparing the and time. Comparing is and the. Comparing the and now. AFTER: is now the time?

    Update: Added punctuation for a bit of fun...

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Seriously, this should be posted with your little test code on the book I picked up! I need to find a better book...

      Now, I play with sub routines and play more with sort...I feel GOOD about Perl this morning...hopefully, some break through in my learning today ;-)

      I've briefly read, rather GLANCED, at a posting while trying to figure out the $a and the $b question I posted here. Any way, File::Compare(I think it was defined like this on google search)module be an option?

      Ringrazio tanto a tutti
Re^5: Need help figuring out how to order/eval the numbers
by perlynewby (Scribe) on Jun 27, 2015 at 02:22 UTC

    I have written little test code that test out the order of the hash BUT ,out of curiosity, I want to call it from this little test program I've been bugging you about, can this be done? and how? I'd appreciate a pointer to read up on it and then I will try it out...I may have questions later but I want to read this first.