Perl Matching multiple variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Matching multiple variables
# 1  
Old 01-08-2014
Perl Matching multiple variables

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.

If all 3 vars are defined compare them.

Just cant see in my minds eye how to do this.

Thank you very much in advance for your help.
# 2  
Old 01-08-2014
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...
# 3  
Old 01-08-2014
This is working I suppose .... kind of crappy though .. I just play around with $a, $b, $c to test behavior. Is there a better way ?


Code:
#!/usr/bin/perl
$a = 3;
$b ;
$c = 3;

  if ((defined $a) && (!defined $b) && (!defined $c)) {};

  if ((defined $a) && (defined $b) && (defined $c)) { 
     print "all defined\n";
      if (!allequal($a, $b, $c)) { 
       print "They're not all equal!\n";
      }
  }

  if ((defined $a) && (defined $b) && (!defined $c)) {
     print "only \$a $a and \$b $b are defined. But not \$c $c\n";
      if (!allequal($a, $b)) {
       print "They're not all equal!\n";
      }
  }

  if ((defined $a) && (!defined $b) && (defined $c)) {
     print "only \$a $a and \$c $c are defined. But not \$b $b\n";
      if (!allequal($a, $c)) {
       print "They're not all equal!\n";
      }
  }


sub allequal {
 my $first = shift;
 for (@_) {
  return 0 unless $first == $_;
 }
 return 1;
}

# 4  
Old 01-08-2014
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;
}

3c7c75f4fa8430aeeefa3e5643701aa2
# 5  
Old 01-08-2014
Quote:
Originally Posted by popeye
... 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!
 
$
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

PERL: In a perl-scripttTrying to execute another perl-script that SETS SOME VARIABLES !

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)
Discussion started by: alex_5161
1 Replies

2. Shell Programming and Scripting

Assigning multiple column's value from Oracle query to multiple variables in UNIX

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)
Discussion started by: hkrishnan91
3 Replies

3. Shell Programming and Scripting

Reading multiple values from multiple lines and columns and setting them to unique variables.

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)
Discussion started by: FMMOLA
7 Replies

4. Shell Programming and Scripting

Running a script with multiple variables like 25 variables.

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)
Discussion started by: Lakshman_Gupta
3 Replies

5. Shell Programming and Scripting

Perl script: matching multiple lines error

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)
Discussion started by: askari
6 Replies

6. Shell Programming and Scripting

Help need with PERL multiple search pattern matching!

My example file is as given below: conn=1 uid=oracle conn=2 uid=db2 conn=3 uid=oracle conn=4 uid=hash conn=5 uid=skher conn=6 uid=oracle conn=7 uid=mpalkar conn=8 uid=anarke conn=9 uid=oracle conn=1 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.10.5.6 to 10.18.6.5 conn=2... (3 Replies)
Discussion started by: sags007_99
3 Replies

7. Shell Programming and Scripting

Help with checking that 2 variables contain matching characters

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)
Discussion started by: lsecer
3 Replies

8. Shell Programming and Scripting

Hidden Characters in Regular Expression Matching Perl - Perl Newbie

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)
Discussion started by: kittyluva2
4 Replies

9. Shell Programming and Scripting

Perl Regex matching multiple lines

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)
Discussion started by: chakrapani
3 Replies

10. Shell Programming and Scripting

perl basic multiple pattern matching

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)
Discussion started by: sinusoid
4 Replies
Login or Register to Ask a Question