in reply to Re^3: Simplify code in Perl with "unless" conditionnal in thread Simplify code in Perl with "unless" condition
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
Re^5: Simplify code in Perl with "unless" conditionnal
by haukex (Archbishop) on May 30, 2016 at 14:01 UTC
|
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 | [reply] [d/l] |
|
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 !
| [reply] |
|
Hi Chaoui05,
Well, to randomize it, you can shuffle both the list of browsers and the list of combinations, that'll ensure that you always have all comparisons but in a random order:
use List::Util 'shuffle';
use Algorithm::Combinatorics 'combinations';
my @browsers = shuffle('firefox', 'chrome', 'internet explorer');
for my $c (shuffle combinations(\@browsers,2)) {
print "Compare $$c[0] with $$c[1]\n";
}
__END__
Compare internet explorer with firefox
Compare firefox with chrome
Compare internet explorer with chrome
Hope this helps, -- Hauke D | [reply] [d/l] |
|
Re^5: Simplify code in Perl with "unless" conditionnal
by choroba (Cardinal) on May 30, 2016 at 12:49 UTC
|
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,
| [reply] [d/l] |
|
I did it but nothing changes. Have i to do it with "shuffle" or there is another way ?
| [reply] |
|
What do you mean by "nothing changes"? I'm getting different output on each run:
#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };
use List::Util qw{ shuffle };
my @browsers = ('firefox', 'chrome', 'internet explorer');
my %screen = map { $_ => "$_\'s screen" } @browsers;
my @random_array = shuffle(@browsers);
for my $b1_idx (0 .. $#random_array - 1) {
my $b1 = $screen{ $random_array[$b1_idx] };
for my $b2_idx ($b1_idx + 1 .. $#random_array) {
my $b2 = $screen{ $random_array[$b2_idx] };
say "About to compare $b1 with $b2";
}
}
($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,
| [reply] [d/l] [select] |
|
|
|
|
Re^5: Simplify code in Perl with "unless" conditionnal
by Corion (Patriarch) on May 30, 2016 at 11:18 UTC
|
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.
| [reply] [d/l] [select] |
|
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.
| [reply] |
|
|