aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-08-29 15:46:41 +0100
committerGitHub <noreply@github.com>2021-08-29 15:46:41 +0100
commite75f77536de7ff2f8fc76718ce668b5d3856a34c (patch)
tree35f4b9cdcd0ae8206b4fd74f690249a91d852e56
parent53dd0e7665c7a62b24e916e43320094c9747d193 (diff)
parent4cce949f19559a638e58d4b661274d65330b72c3 (diff)
downloadperlweeklychallenge-club-e75f77536de7ff2f8fc76718ce668b5d3856a34c.tar.gz
perlweeklychallenge-club-e75f77536de7ff2f8fc76718ce668b5d3856a34c.tar.bz2
perlweeklychallenge-club-e75f77536de7ff2f8fc76718ce668b5d3856a34c.zip
Merge pull request #4801 from corvettes13/patch-5
Create ch-1.pl
-rw-r--r--challenge-127/paul-fajman/perl/ch-1.pl54
1 files changed, 54 insertions, 0 deletions
diff --git a/challenge-127/paul-fajman/perl/ch-1.pl b/challenge-127/paul-fajman/perl/ch-1.pl
new file mode 100644
index 0000000000..e846efc14c
--- /dev/null
+++ b/challenge-127/paul-fajman/perl/ch-1.pl
@@ -0,0 +1,54 @@
+#!/usr/bin/perl
+
+# Weekly Challenge 127 Task #1
+# You are given two sets with unique integers.
+# Write a script to figure out if they are disjoint.
+
+use strict;
+use warnings;
+
+my @set1 = qw (1 2 5 3 4);
+my @set2 = qw (4 6 7 8 9);
+
+my @set3 = qw (1 3 5 7 9);
+my @set4 = qw (0 2 4 6 8);
+
+my @test=(1,1); my $num; my $i;
+
+# Set this up to pass array reference to subroutine
+my $set1_ref = \@set1;
+my $set3_ref = \@set3;
+my $set2_ref = \@set2;
+my $set4_ref = \@set4;
+my @set_arrays = ($set1_ref, $set2_ref, $set3_ref, $set4_ref);
+
+my $array_ref1; my @array_ref1;
+my $array_ref2; my @array_ref2;
+
+# Loop over all the arrays for comparison. Count by two as comparison is between 2 arrays.
+for ($i=0; $i<$#set_arrays; $i+=2) {
+ $array_ref1 = $set_arrays[$i];
+ @array_ref1 = @$array_ref1;
+ $array_ref2 = $set_arrays[$i+1];
+ @array_ref2 = @$array_ref2;
+
+ # Loop over values from the array;
+ foreach(@array_ref1) {
+ $num = $_; # Value to test
+ if (grep /$num/, @array_ref2) { # Test if the value is in the array.
+ $test[0] = 0;
+ $test[1] = $num;
+ last;
+ }
+ }
+ # Print the output
+ print "\nInput: \@S1 = (@array_ref1)\n";
+ print " \@S2 = (@array_ref2)\n";
+ if ($test[0] eq 0) {
+ print "Output: $test[0] as the given two sets have common member $test[1]\n";
+ }
+ else {
+ print "Output: $test[0] as the given two sets do no have a common member\n";
+ }
+ @test=(1,1);
+}