Perl Array Condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Array Condition
# 8  
Old 01-03-2011
You're right, sorry.
I forgot to empty the hash Smilie

Use this:

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

# 9  
Old 01-03-2011
try below:-


Code:
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);
'

# 10  
Old 01-03-2011
Yet another one Smilie

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

Code:
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?
# 11  
Old 01-04-2011
Code for one array

Many thanks Radoulov,

the following code is working well:
Quote:
#!/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";
}
}
Could you please provide the code for only one array?

Thanks in advance.
# 12  
Old 01-04-2011
Use a subroutine. Declare this code somewhere at the beginning of your script:


Code:
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:
Code:
if (is_unique @array1) {
  ...
  }

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