# Array elements comparison using perl

**URL:** <https://community.unix.com/t/array-elements-comparison-using-perl/327000>\
**Category:** Shell Programming and Scripting\
**Created:** [April 12, 2013, 12:20am UTC](https://community.unix.com/t/array-elements-comparison-using-perl/327000 "2013-04-12T00:20:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![solaix14](https://community.unix.com/letter_avatar/solaix14/32/5_5575768a8748004e209b776fc1b2916d.png) [@solaix14](https://community.unix.com/u/solaix14)\
**Post date:** [April 12, 2013, 12:20am UTC](https://community.unix.com/t/array-elements-comparison-using-perl/327000/1 "2013-04-12T00:20:13Z")

</div>

Experts,  
I am looking to compare elements of 2 array using perl. Below is not the actual code but logic wise something like this.

```nohighlight
my $version = "MYSQlcl-5.2.4-264.x86_64"; <-- split this word into array as (5 2 4 264) ( which is to extract only the version number from the package name)

my @array1 = ( 5 2 4 264); <-- after splitting from above
my @array2 = ( 5 3 2 112); <-- pre defined array

if (@array1[0] = @array2[0]) and (@array1[1] = @array2[1]) and (@array1[2] = @array2[2]) and (@array1[3] = @array2[3])
    {
     print "Same. No upgrade required\n";
     }
elsif (@array1[0] > @array2[0]) and (@array1[1] > @array2[1]) and (@array1[2] > @array2[2]) and (@array1[3] > @array2[3])
    {
     print "Already at higher level. No upgrade required\n";
     }
else 
     {
     print "Found at lower version. Upgrade required. Upgrading\n";
     }

```

thanks in advance.

---

<div class="post-metadata">

**Author:** ![guruprasadpr](https://community.unix.com/user_avatar/community.unix.com/guruprasadpr/32/1542_2.png) [@guruprasadpr](https://community.unix.com/u/guruprasadpr)\
**Post date:** [April 12, 2013, 12:41am UTC](https://community.unix.com/t/array-elements-comparison-using-perl/327000/2 "2013-04-12T00:41:51Z")

</div>

Like this:

```nohighlight
my $version = "MYSQlcl-5.2.4-264.x86_64";
my @array1=$version=~/\d+/g;
my @array2 = (5,3,2,112);
my $ver1=join '',@array1[0..3];
my $ver2=join '',@array2;
if ($ver1 == $ver2){
        print "Same. No upgrade required\n";
}elsif ($ver1 > $ver2){
        print "Already at higher level. No upgrade required\n";
}else{
        print "Found at lower version. Upgrade required. Upgrading\n";
}

```

Guru.
