Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: Simplify code in Perl with "unless" conditionnal

by Corion (Patriarch)
on May 30, 2016 at 10:49 UTC ( [id://1164488]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Simplify code in Perl with "unless" conditionnal
in thread Simplify code in Perl with "unless" condition

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.

Replies are listed 'Best First'.
Re^4: Simplify code in Perl with "unless" conditionnal
by Chaoui05 (Scribe) on May 30, 2016 at 11:12 UTC
    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

      Hi Chaoui05,

      From experience I can tell you that it's not always a good idea to randomize tests (it causes random failures and random successes, different on each run, not easily reproducible), instead making them deterministic. Why do you want to pick random browsers to compare?

      This compares each browser to each other browser:

      use Algorithm::Combinatorics 'combinations'; my @browsers = ('firefox', 'chrome', 'internet explorer'); my $iter = combinations(\@browsers,2); while (my $c = $iter->next) { print "Compare $$c[0] with $$c[1]\n"; } __END__ Compare firefox with chrome Compare firefox with internet explorer Compare chrome with internet explorer
      I never know if i give enough information or not at each time.

      As a basis you can always do the same thing: post short but runnable code that reproduces the problem along with short sample input, the output you would expect from that code (written by hand if necessary), and the output you're actually getting, including exact error messages. This is explained, for example, in How do I post a question effectively? and Short, Self Contained, Correct Example. If you're unsure, ask yourself, what information do we need to easily reproduce the problem you are seeing?

      Hope this helps,
      -- Hauke D

        Thanks for the reply, it's cool.

        First i want to do a randomize comparison test in the objective to not have same difference' screenshot at each time. And moreover to have different filename. Thanks for your approach , but it seems that we have always first browser with following and the following with following as we can see in your output ? And i would like to get them randomly for my part.

        Yes , in any case, here i have enough information. Thanks

        Concerning the way to post , i always try to explain the case with the maximum of information i.e. with the input and maximum of code as it's possible and with the output. And of course with explanations. iam using posts edited here, in perlmonks.

        Thanks again !

        Lost in translation
      If you want to replace @browsers with @random_array, you need to do it everywhere. You only used the random array to get the indices, which are in fact the same as in the original. Replace all mentions of @browsers by @random_array (except the one where you shuffle it, of course).

      ($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,
        I did it but nothing changes. Have i to do it with "shuffle" or there is another way ?
        Lost in translation

      The code you show above does not work stand-alone.

      Please remove all parts that are not relevant to the problem at hand.

      If your problem is to rearrange the elements of @browsers in a random fashion, write a program that shows the array before being shuffled, shuffle it and then show the array after having been shuffled.

      The next step is then to inspect whether the array is getting shuffled as you want it.

      The next step after you are certain that the shuffling in itself works is to apply the new shuffled values to your code.

      Have you verified that you are using the shuffled elements? For debugging, printing all values at every step is often helpful to show the values as your program progresses through the program.

        You said "If your problem is to rearrange the elements of @browsers in a random fashion, write a program that shows the array before being shuffled, shuffle it and then show the array after having been shuffled." . Not exactly. I don't want to rearrange the elements , but take them randomly for each comparison i have to do. As i said above. Thanks for debug idea.
        Lost in translation

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1164488]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-26 04:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found