in reply to Simplify code in Perl with "unless" condition

We usually don't call the block that unless introduces a "loop", as it isn't repeated.

You can use loops to iterate over the browsers, though, and you can use variables to avoid fetching the hash values every time:

my @browsers = ('firefox', 'chrome', 'internet explorer'); for my $b1_idx (0 .. $#browsers - 1) { my $b1 = $screen{ $browsers[$b1_idx] }; for my $b2_idx ($b1_idx + 1 .. $#browsers) { my $b2 = $screen{ $browsers[$b2_idx] }; unless ($b1->compare($b2)) { my $diff_file = $b1->difference($b2); print "#The images are not the same; see $diff_file for de +tails\n"; qx{ $diff_file }; } } }

Update: missing +1 added.

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: Simplify code in Perl with "unless" conditionnal
by Chaoui05 (Scribe) on May 27, 2016 at 16:25 UTC
    I meaned a condition choroba Loop is a good solution. I must continue to dig which approach could be the best. Thanks
    Lost in translation
Re^2: Simplify code in Perl with "unless" conditionnal
by Chaoui05 (Scribe) on May 30, 2016 at 10:40 UTC
    I try to use your approach. I would like now to get an random element from the list . I did this :
    use List::Util qw(shuffle); my @random_array = shuffle(@browsers); for my $b1_idx (0 .. $#random_array - 1)
    But it seems to not work. Is it correct or is there an other way to do it ? Thanks in advance !
    Lost in translation

      What do you mean by "it seems to not work"?

      You can help us improve the quality of the answers we're giving by answering the following questions directly in your first post:

      What is the exact code you are running? Please show the exact code. This code should be self-sufficient and be shorter than 20 lines.

      What is the exact input you are giving? Please show the exact input you are giving to the above program.

      What is the output you expect? Please describe the output you expect.

      What is the exact output you get? Please show the exact output together with the complete error message(s) that Perl returns you.

      Why are you using List::Util? Have you read perlfaq or run perldoc -q random ? This will show you the many frequently asked questions (and their answers) pertaining to getting a random element of an array.

        Much for me. I never know if i give enough information or not at each time. By saying "it seems to not work" i mean there are no error output but it doesn't do what i expected i.e. take on a random element from the list to compare it with the following. Iam using List::Util to try to get a random element also. And for the code , i just used choroba input , above.
        my @browsers = ('firefox', 'chrome', 'internet explorer'); my @random_array = shuffle(@browsers); for my $b1_idx (0 .. $#random_array - 1) { my $b1 = $screen{ $browsers[$b1_idx] }; for my $b2_idx ($b1_idx .. $#browsers) { my $b2 = $screen{ $browsers[$b2_idx] }; unless ($b1->compare($b2)) { my $diff_file = $b1->difference($b2); print '#The images are not the same; see' . $diff_file .' fo +r details'."\n"; qx{ $diff_file }; } } }

        Doing that, my goal is to get a random element from list :

        my @browsers = ('firefox', 'chrome', 'internet explorer');

        For example, compare at first 'chrome' screenshot with 'internet explorer' screenshot and after 'firefox' with 'chrome' etc.

        Thanks
        Lost in translation
Re^2: Simplify code in Perl with "unless" conditionnal
by Chaoui05 (Scribe) on May 30, 2016 at 14:17 UTC
    Ok for the update
    Lost in translation