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

Hello again perlynewby,

It seems that, between aaron_baugher’s excellent replies and your own further reading, you already have answers to most of your questions. I will just comment on this part of my code:

my $fh = defined $hash{$ita}[0] && defined $hash{$ita}[1] ? $out : $out1; print $fh "$ita => ", join(',', map { $_ // () } @{ $hash{$ita} }), "\n";

The ... ? ... : ... construct is the conditional, or ternary, operator, which is documented here. I use it to select which of two filehandles will be written to by the following print statement. This is an example of applying the DRY (“don’t repeat yourself”) principle by changing the original two parallel print statements into a single statement. The advantage here is that if the statement is later changed (see below!), there is no danger of updating one statement and overlooking the other in the process.

The expression { $_ // () } uses the Logical Defined-Or operator to select the empty parentheses — () — only if the current value ($_) is undefined. Empty parentheses are used because they represent an empty list, and interpolating an empty list into a second list has no effect on that second list. Any other value here — e.g. undef, "" (the empty string), [] (which is a reference to an empty anonymous array) — would add something unwanted to the second list, because in each case the value is a scalar which is added as a new list element. The following should make this a little clearer:

17:42 >perl -MData::Dump -wE "my @c = qw(a e i o u); @c = map { $_ eq +'i' ? undef : $_ } @c; dd \@c;" ["a", "e", undef, "o", "u"] 17:44 >perl -MData::Dump -wE "my @c = qw(a e i o u); @c = map { $_ eq +'i' ? () : $_ } @c; dd \@c;" ["a", "e", "o", "u"] 17:44 >
map...nice function, I'm geeking out on map right now.

Yor’re right! Unfortunately, map was the wrong function to use here. :-( I should have used grep:

print $fh "$ita => ", join(',', grep { defined } @{ $hash{$ita} }), "\n";

— much more straightforward. D’oh!

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^4: Need help figuring out how to order/eval the numbers
by perlynewby (Scribe) on Jun 23, 2015 at 23:00 UTC

    excellent! I liked the break down of the expression...I totally had some of this wrong in my head

    this explanations were very insightful and removed misconception that I may have had...that's not to say I will get it right on my next try(s) to modify/learn perl code.

    will think of a program to use what I have learn from you...or you can provide me with an idea to utilize these new learnings too...and so expect a little more questions

    again Grazie MILLE

Re^4: Need help figuring out how to order/eval the numbers
by perlynewby (Scribe) on Jun 25, 2015 at 00:56 UTC

    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... }

      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

      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.