Perl Array Condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Array Condition
# 1  
Old 01-03-2011
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:
Code:
@array1 = qw(44 44 44 44);
@array2 = qw(55 55 55 55 55 55 55);

Please advice,
Ahmed
# 2  
Old 01-03-2011
Code:
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
    '


Last edited by radoulov; 01-03-2011 at 03:16 PM.. Reason: Corrected!
This User Gave Thanks to radoulov For This Post:
# 3  
Old 01-03-2011
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
# 4  
Old 01-03-2011
The code is written in Perl, but it uses a features of version 5.10.

Put this code in a script and try it:

Code:
#! /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 ----------

Last edited by radoulov; 01-03-2011 at 03:17 PM.. Reason: Corrected!
This User Gave Thanks to radoulov For This Post:
# 5  
Old 01-03-2011
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
# 6  
Old 01-03-2011
Quote:
Also I tried to change the value of one element of the array to be different values but I got the same output !!!!!!
Could you post an example? I mean, the exact code you run.

This is supposed to be a readable version:
Code:
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";
    }  
  }

This User Gave Thanks to radoulov For This Post:
# 7  
Old 01-03-2011
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:

Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

If condition with array

hi, say if my array has the following integer values.array=( 5 10 15 20 )and A assigns to 5.A=5How do I if condition to check against if value A is in this array or not, in shell script? thx (5 Replies)
Discussion started by: martin5kh
5 Replies

2. Shell Programming and Scripting

ksh : Building an array based on condition result

I want to build an Errorlog. I would like to build an array as I move through the if statements and print the array once all error conditions have been defined. The results need to be comma delimited. tsver will be static "1.9.6(2)" other vars $prit $lt $rt can have the same or a different... (1 Reply)
Discussion started by: popeye
1 Replies

3. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

4. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

5. Shell Programming and Scripting

Array in Perl - Detect several file to be in one array

Hi everyone I have one question about using array in perl. let say I have several log file in one folder.. example test1.log test2.log test3.log and the list goes on.. how to make an array for this file? It suppose to detect log file in the current directory and all the log file will... (3 Replies)
Discussion started by: sayachop
3 Replies

6. Shell Programming and Scripting

perl, put one array into many array when field is equal to sth

Hi Everyone, #!/usr/bin/perl use strict; use warnings; my @test=("a;b;qqq;c;d","a;b;ggg;c;d","a;b;qqq;c;d"); would like to split the @test array into two array: @test1=(("a;b;qqq;c;d","a;b;qqq;c;d"); and @test2=("a;b;ggg;c;d"); means search for 3rd filed. Thanks find the... (0 Replies)
Discussion started by: jimmy_y
0 Replies

7. Shell Programming and Scripting

IF condition against a ARRAY in shell script

Hi, I want to check a particular string inserted by User to be checked against the values i already have in a ARRAY string using IF condition. Is this possible? if yes how to do that. example : i have a,b,c,d,e,f values in a array called values i asked user to enter a value: user entered... (2 Replies)
Discussion started by: kukretiabhi13
2 Replies

8. Shell Programming and Scripting

Perl grep array against array

Hi, Is there any way I can grep an array against another array? Basically here's what I need to do. There will be an array containing some fixed texts and I have to check whether some files contain these lines. Reading the same files over and over again for each different pattern doesnt seem... (1 Reply)
Discussion started by: King Nothing
1 Replies

9. Shell Programming and Scripting

awk - array elements as condition

Hi, can I use array elements ( all ) in conditional statements? the problem is ,the total number of elements is not known. e.g A is an array with elements - 1,2,3 now if i want to test if the 1 st field of input record is either 1,2 or 3, i can do something like this if ( $1 ~... (1 Reply)
Discussion started by: shellwell
1 Replies

10. Shell Programming and Scripting

[Perl] Accessing array elements within a sed command in Perl script

I am trying to use a script to replace the header of each file, whose filename are stored within the array $test, using the sed command within a Perl script as follows: $count = 0; while ( $count < $#test ) { `sed -e 's/BIOGRF 321/BIOGRF 332/g' ${test} > 0`; `cat 0 >... (2 Replies)
Discussion started by: userix
2 Replies
Login or Register to Ask a Question