I am trying to match mulitple (3) variables. I found the sub given below on the web which works well when all vars are defined. But there are situations where one or two will not be defined (at least one will always be defined.)
Example of the variable content possibilities
Quote:
$p = 1676:790 #works good with sub
$l = 1676:790
$r = 1676:790
$p = 1676:790 #works good with sub
$l = 1763:890
$r = 1676:790
$p = 1676:790 #doesnt work as expected.
$l
$r
$p = 1676:790 #doesnt work as expected. Gives the "not" response when It should actually be "ok."
$l = 1676:790
$r
The sub found on the web.
Code:
if (allequal($hpver, $hlver, $hrver)) {
print "They're all equal!\n"
} else {
print "They're not all equal!\n";
}
sub allequal {
my $first = shift;
for (@_) {
return 0 unless $first == $_;
}
return 1;
}
If only one var is defined, then I dont want to do anything (no other var to compare)
If 2 or more vars are defined compare them, ignore the undefined.
What is known as variable substitution is your friend for such cases, you substitue a default value rather then ending with a variable not set.
Syntax:
Code:
${variable:-defaultvalue}
Addendum:
Sorry I should have read to the end - Did not see you were in perl...
Here's another version that may help you. You can test it by changing one of the values to "".
Code:
#!/usr/bin/perl
#
#
#
use strict;
use diagnostics;
# set vars
my $p = "1676:790";
my $l = "1763:890";
my $r = "1676:790";
my $retval = 0;
# test to see if all the strings have a value
if((&checkVal($p, $l, $r)) eq 1) {
print "They're not equal!\n";
} else {
print "They're equal!\n";
}
# subroutine to perform the test
sub checkVal {
# read values into an array
# this will capture even the
# empty values
my @values = @_;
foreach(@values) {
# test if a value is empty and set
# the return value to 1
if($_ eq "") {
$retval = 1;
}
}
return $retval;
}
... But there are situations where one or two will not be defined (at least one will always be defined.)
...
If only one var is defined, then I dont want to do anything (no other var to compare)
If 2 or more vars are defined compare them, ignore the undefined.
If all 3 vars are defined compare them.
...
If only one var is defined, I assume you want to throw an exception.
Code:
$
$ cat -n test_equality.pl
1 #!perl -w
2 use strict;
3
4 # ========================
5 # Subroutines section
6 # ========================
7
8 sub allequal {
9 my ($p, $l, $r) = @_; # Assign to local variables
10
11 my $all = join("", @_); # Join them all
12 # $all cannot be "" since at least one var is defined
13 if ($p eq $all or $l eq $all or $r eq $all) { # If any two are NULLs then die
14 die "Only one var is defined; nothing to do!"
15 }
16
17 if ("" eq $p // "") { # If only one is defined then return
18 return $l eq $r; # the result of comparison of the other two
19 } elsif ("" eq $l // "") {
20 return $p eq $r;
21 } elsif ("" eq $r // "") {
22 return $p eq $l;
23 }
24
25 return $p eq $l && $l eq $r && $r eq $p; # All are non NULLs, hence compare them all
26 }
27
28 # ========================
29 # Main section
30 # ========================
31 print "Enter value of \$hpver : ";
32 chomp(my $hpver = <STDIN>);
33
34 print "Enter value of \$hlver : ";
35 chomp(my $hlver = <STDIN>);
36
37 print "Enter value of \$hrver : ";
38 chomp(my $hrver = <STDIN>);
39
40 print "You entered: (\$hpver = [$hpver], \$hlver = [$hlver], \$hrver = [$hrver])\n";
41
42 if (allequal($hpver, $hlver, $hrver)) {
43 print "They're all equal!\n"
44 } else {
45 print "They're not all equal!\n";
46 }
47
$ perl test_equality.pl
Enter value of $hpver : 1676:790
Enter value of $hlver : 1676:790
Enter value of $hrver : 1676:790
You entered: ($hpver = [1676:790], $hlver = [1676:790], $hrver = [1676:790])
They're all equal!
$ perl test_equality.pl
Enter value of $hpver : 1676:790
Enter value of $hlver : 1763:890
Enter value of $hrver : 1676:790
You entered: ($hpver = [1676:790], $hlver = [1763:890], $hrver = [1676:790])
They're not all equal!
$ perl test_equality.pl
Enter value of $hpver : 1676:790
Enter value of $hlver :
Enter value of $hrver :
You entered: ($hpver = [1676:790], $hlver = [], $hrver = [])
Only one var is defined; nothing to do! at test_equality.pl line 14, <STDIN> line 3.
$ perl test_equality.pl
Enter value of $hpver : 1676:790
Enter value of $hlver : 1676:790
Enter value of $hrver :
You entered: ($hpver = [1676:790], $hlver = [1676:790], $hrver = [])
They're all equal!
$ perl test_equality.pl
Enter value of $hpver :
Enter value of $hlver : 1676:790
Enter value of $hrver : 1763:890
You entered: ($hpver = [], $hlver = [1676:790], $hrver = [1763:890])
They're not all equal!
$
$
I have reviewed many examples on-line about running another process (either PERL or shell command or a program), but do not find any usefull for my needs way. (Reviewed and not useful the system(), 'back ticks', exec() and open())
I would like to run another PERL-script from first one, not... (1 Reply)
Hi All,
I need to read values of 10 columns from oracle query and assign the same to 10 unix variables. The query will return only one record(row).
I tried to append all these columns using a delimiter(;) in the select query and assign the same to a single variable(V) in unix. I thought I... (3 Replies)
Hello,
I would like to ask for help with csh script.
An example of an input in .txt file is below, the number of lines varies from file to file and I have 2 or 3 columns with values. I would like to read all the values (probably one by one) and set them to independent unique variables that... (7 Replies)
Hi All,
i have a requirement where i have to run a script with at least 25 arguements and position of arguements can also change. the unapropriate way is like below. can we achieve this in more good and precise way??
#!/bin/ksh
##script is sample.ksh
age=$1
gender=$2
class=$3
.
.
.... (3 Replies)
Dear Perl users,
Could somebody help me how to fix my code so I can get my desired output.
Here is the data:
Pattern Gabriel
halo1
halo2
end
Pattern Andreas
halo1
halo2
endI want to grep multiple lines between the pattern /Pattern Gabriel / and /end/.
Then I will store the output into... (6 Replies)
hi
i am writing a hangman script and am having trouble checking the correct letters against the word
i need the script to compare the word against the letters guessed that are correct so once all the letters within the word have been guessed it will alow me to create a wining senario
eg
... (3 Replies)
I am completely new to perl programming. My father is helping me learn said programming language. However, I am stuck on one of the assignments he has given me, and I can't find very much help with it via google, either because I have a tiny attention span, or because I can be very very dense.
... (4 Replies)
I need a way to extract data from
X 4T Solution 21 OCT 2011 37 .00
to account 12345678 User1 user2
X 4T Solution Solution Unlimited 11 Sep 2009 248 .00
to account 87654321 user3 user4
I need it to extract 'X' '37.00' and account number 12345678.
I have extracted above stuff... (3 Replies)
Hi everyone, and thank you for your help with this. I am VERY new with perl so all of your help is appreciated. I have tried google but as I don't know the proper terms to search for and could be daunting for a newbie scripter... I know this is very easy for most of you! Thanks!
I have a... (4 Replies)