Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

by Chaoui05 (Scribe)
on May 30, 2016 at 10:40 UTC ( [id://1164487]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^3: Simplify code in Perl with "unless" conditionnal
by Corion (Patriarch) on May 30, 2016 at 10:49 UTC

    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

        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

        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,

        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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-03-28 20:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found