perl - get uniq item from an array?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl - get uniq item from an array?
# 1  
Old 06-26-2012
perl - get uniq item from an array?

practicing perl now and hope to get uniq item from an array:

my current work:

Code:
#!/usr/local/bin/perl
my @source = ("aaa", "aaa", "bbb", "ccc", "ddd");

my $index=0;
my @uniq;

foreach (@source)
{
  chomp;

  # push first item to @uniq
  if ($index == 0)
  {
     push @uniq, $_;
  }

  else
  {
     my $count = grep /$_/, @uniq;
     if ($count <= 0)
     {  
        push @uniq, $_;
     }
  }
  $index++;
}

## print the @uniq
foreach (@uniq)
{
  chomp;
  print $_, "\n";
}

this only prints one line:
aaa

while i expect it prints:
aaa
bbb
ccc
ddd

any advice on this?? Thanks.
# 2  
Old 06-26-2012
Code:
#!/usr/local/bin/perl
my @source = ("aaa", "aaa", "bbb", "ccc", "ddd");

my $index=0;
my @uniq;
my $e;

foreach $e (@source) # Good practice to use variables instead of default $_
{
  # chomp; # Not required. Elements of @source do not contain a trailing newline character (input record separator).

  # push first item to @uniq
  if ($index == 0)
  {
    push @uniq, $e;
  }

  else
  {
    my $count = grep /$e/, @uniq;
    if ($count <= 0)
    {  
       push @uniq, $e;
    }
  }
  $index++;
}

## print the @uniq
foreach (@uniq)
{
  # chomp; # Not required. Elements of @source do not contain a trailing newline character (input record separator).
  print $_, "\n";
}

# You can as well use print "@uniq"; instead of the above foreach loop

---------- Post updated at 14:22 ---------- Previous update was at 14:12 ----------

A quicker way is to use a hash.

Code:
#! /usr/bin/perl -w
use strict;

my @source = ("aaa", "aaa", "bbb", "ccc", "ddd");
my %uniq;

for (@source) {
    $uniq{$_}++;
}

for (sort keys %uniq) {
    print "$_ -> $uniq{$_}\n";
}

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 06-26-2012
Try this also,

Code:
#!/usr/local/bin/perl
my @source = ("aaa", "aaa", "bbb", "ccc", "ddd");
foreach my $var (@source)
{
   push @uniq,$var if(!grep /$var/,@uniq);
}
print "My uniq array:: <@uniq> \n";

Regards
Indu
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash shell script undefined array item value question

Hello, I'm new here. I test these expressions's value in my script : (in centOS 6 ) #!/bin/bash array='something' echo "############" echo ${array} echo ${array} echo ${array} echo "############" The output result is : ################# something something #################... (5 Replies)
Discussion started by: lingjing
5 Replies

2. Shell Programming and Scripting

Associative Array with more than one item per entry

Hi all I have a problem where i have a large list ( up to 1000 of items) and need to have 2 items pulled from it into variables in a bash script my list is like the following and I could have it as an array or possibly an external text file maintained separately. Every line is different and... (6 Replies)
Discussion started by: kcpoole
6 Replies

3. Shell Programming and Scripting

Removing Dupes from huge file- awk/perl/uniq

Hi, I have the following command in place nawk -F, '!a++' file > file.uniq It has been working perfectly as per requirements, by removing duplicates by taking into consideration only first 3 fields. Recently it has started giving below error: bash-3.2$ nawk -F, '!a++'... (17 Replies)
Discussion started by: makn
17 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

retrieve what the currently selected item is in a dropdown select list using perl tk

I have a dropdown menu built in perl tk (I am using active state perl). I want to select a value from the dropdown menu and I want to be able to perform some other actions depending upon what value is selected. I have all the graphical part made but I dont know how to get the selected value. Any... (0 Replies)
Discussion started by: lassimanji
0 Replies

8. 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

9. Shell Programming and Scripting

find an available item in array

Dear all, I'm have a sorted array like this: 177 220 1001 2000 2001 2003 2005 notice that 2002 and 2004 are NOT in array. Then user input a number INPUT, our script should return OUTPUT value like this: if INPUT is not in array => OUTPUT=INPUT if INPUT is in array => OUTPUT is the... (4 Replies)
Discussion started by: fongthai
4 Replies

10. Shell Programming and Scripting

sort and uniq in perl

Does anyone have a quick and dirty way of performing a sort and uniq in perl? How an array with data like: this is bkupArr BOLADVICE_VN this is bkupArr MLT6800PROD2A this is bkupArr MLT6800PROD2A this is bkupArr BOLADVICE_VN_7YR this is bkupArr MLT6800PROD2A I want to sort it... (4 Replies)
Discussion started by: reggiej
4 Replies
Login or Register to Ask a Question