w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
How to print common values from two different overlapping ranges without repetition

The first step to understanding your problem is to understand your data — the arrays do not hold what you think they hold.

#!/usr/bin/perl
my @arr2=(1..10,5..15,10..20);
my @arr4=(5..9,13..18);

print "arr2: @arr2
";
print "arr4: @arr4
";

The output from this is:

arr2: 1 2 3 4 5 6 7 8 9 10 5 6 7 8 9 10 11 12 13 14 15 10 11 12
13 14 15 16 17 18 19 20
arr4: 5 6 7 8 9 13 14 15 16 17 18

This shows that your code trying to split a string on .. is going to fail horribly.

One of the most basic debugging techniques is printing out the data you've actually got to ensure it matches what you think you should have. Here, that basic printing would have shown that the input data is not in the format you expected.





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.