Perl Array Condition

Hello,

I want to check if all element of an array have the same value regardless the length of the array.

example:

@array1 = qw(44 44 44 44);
@array2 = qw(55 55 55 55 55 55 55);

Please advice,
Ahmed

perl -E'
  @array1 = qw(44 44 44 44);
  @array2 = qw(55 55 55 55 55 55 56);
  
  do {
    %_ = ();
    @_{@$_} = ();
    say "@$_ -> ", keys %_ > 1 ? "KO" : "OK"
      } for \@array1, \@array2
    '
1 Like

Thanks for your reply.

I am beginner in perl, but do you confirm that this code is written in perl?
I got many error when I execute it.

Please advise,
Ahmed

The code is written in Perl, but it uses a features of version 5.10.

Put this code in a script and try it:

#! /usr/bin/perl 

use warnings;
use strict;

my @array1 = qw(44 44 44 44);
my @array2 = qw(55 55 55 55 55 55 56);

do {
    %_ = ();
    @_{@$_} = ();
    print "@$_ -> ", keys %_ > 1 ? "KO" : "OK", "\n"
      } for \@array1, \@array2;

---------- Post updated at 05:17 PM ---------- Previous update was at 05:13 PM ----------

1 Like

Many thanks for swift response.

It works without errors, but where should I put the actions if all elements have the same value?
and if they don't?

Also I tried to change the value of one element of the array to be different values but I got the same output !!!!!!

Actually, I have one array and I want to assure that all elements in the array have the same value regardless the length of it, then take a specific action.

Appreciate you reply and thank you again,
Ahmed

Could you post an example? I mean, the exact code you run.

This is supposed to be a readable version:

use warnings;
use strict;

my @arrys = (
  [ qw( 44 44 44 44 ) ],
  [ qw( 55 55 55 55 55 55 56 ) ],
  );

my %h;  
  
for my $arry (@arrys) {
  $h{$_} = undef for @$arry;
  if (keys %h > 1) {
    print "@$arry is not OK\n"
    }
  else {
    print "@$arry is OK\n";
    }  
  }  
1 Like

Thanks again,

I still have the same output after changing the last element of 2nd array from "56" to "55" to make all have the same value but unfortunatly i got the same output:

C:\Users\Administrator>perl c:\test.pl
44 44 44 44 is OK
55 55 55 55 55 55 56 is not OK
 
C:\Users\Administrator>perl c:\test.pl
44 44 44 44 is OK
55 55 55 55 55 55 55 is not OK

You're right, sorry.
I forgot to empty the hash :slight_smile:

Use this:

#!/usr/bin/perl

use warnings;
use strict;

my @arrys = (
  [ qw( 44 44 44 44 ) ],
  [ qw( 55 55 55 55 55 55 56 ) ],
  );

  
for my $arry (@arrys) {
  my %h;
  $h{$_} = undef for @$arry;
  if (keys %h > 1) {
    print "@$arry is not OK\n"
    }
  else {
    print "@$arry is OK\n";
    }  
  }

try below:-

perl -le'
  my @array1 = qw(44 44 414 44);
  my @array2 = qw(55 55 55 55 53 55 55);
  map { map { $_{$_} = undef } @$_ ; print keys %_ > 1 ? KO : OK ;%_=() ;} (\@array1, \@array2);
'

Yet another one :slight_smile:

perl -le'
  @array1 = qw(44 44 414 44);
  @array2 = qw(55 55 55 55 55 55 55);
  
  do {
      local @_{@$_} = ();
      print keys %_ > 1 ? "KO" : "OK"
      } for \@array1, \@array2
    '

---------- Post updated at 09:57 PM ---------- Previous update was at 09:56 PM ----------

With no spaces :slight_smile:

perl -le'
  @array1 = qw(44 44 414 44);
  @array2 = qw(55 55 55 55 55 55 55);
  
  do{local@_{@$_}=();print keys%_>1?"KO":"OK"}for\@array1,\@array2
    
    '

Could we make it shorter?

Many thanks Radoulov,

the following code is working well:

Could you please provide the code for only one array?

Thanks in advance.

Use a subroutine. Declare this code somewhere at the beginning of your script:

sub is_unique (\@) {
  my ($a, %h) = shift;
  $h{$_} = undef for @$a;
  return keys %h > 1 ? 0 : 1;
  }

So you can call it wherever you want:

if (is_unique @array1) {
  ...
  }