Visit Our UNIX and Linux User Community


Not able to read unique values in array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Not able to read unique values in array
# 1  
Old 09-08-2009
Not able to read unique values in array

Hi Friends,

I am having some trouble reading into an array. Basically, I am trying to grep for a pattern and extract it's value and store the same into an array. For eg., if my input is:
Code:
<L:RECORD>name=faisel farooq,age=21,
company=TCS,project=BT</L:RECORD>
<L:RECORD>name=abc xyz,age=22,company=Infosys Tech,project=AT</L:RECORD>
<L:RECORD>name=uresh kum,
age=20,company=TCS L,project=VGP</L:RECORD>
<L:RECORD>name=maha laxmi,age=23,company=Tata Co,project=JP Morgan and STanly</L:RECORD>

I grep for 'age' and want to extract 21 and store it in the array.
Here's the code I have come up with:
Code:
#/bin/perl
$search="Seq";
$seqCount=0;
@seqArray;
$found=0;
open(READ,"op.txt") or die "Can not open file: $!\n";
@array=<READ>;
close READ;
sub checkExist {
        my $a=$_[0];
        for($i=0;$i<=$seqCount;$i++) {
                if($seqArray[$i]==$a) {
#                       $found=1;
                        return 1;
                }
        }
}
foreach $line(@array)
{
        if ($line =~ /$search/)
        {
                $,="\n";
                @sa=split(/([=,])/,$line);
                $count=0;
                print"@sa\n";
                $length=@sa;
                for($i=0;$i<$length;$i++)
                {
                        $found=0;
                        print "Loop $i: $sa[$i]\n";
                        if($sa[$i] =~ /$search/)
                        {
                                if(!checkExist($sa[$i+2]))
                                {
                                        $seqArray[$seqCount]=$sa[$i+2];
                                        print"Bus Txn Seq is: $sa[$i+2]\n";
                                        $seqCount++;
                                }
                        }
                }
        }
}
print "@seqArray\n";

This does not seem to be working, and the program is going into infinite loop.
One thing I noticed is, if i comment out the below if loop:
Code:
if($sa[$i] =~ /$search/)
                        {
                           #     if(!checkExist($sa[$i+2]))
                             #   {
                                        $seqArray[$seqCount]=$sa[$i+2];
                                        print"Bus Txn Seq is: $sa[$i+2]\n";
                                        $seqCount++;
                            #    }
                        }

then the contents do get stored in the array, but not uniquely. Means to say, if i have more records having age=21, then 21 gets stored in the array as many times as it is present in the input file. I would like this to be avoided, and no matter how many times age=21 is present, it should be stored just once.
Thanks in advance for helping..
# 2  
Old 09-08-2009
Not going to bother looking into the guts of what you're trying to do. When you say your code is not working and going into an infinite loop, I don't know how that can be. I understand it doesn't work but I don't know how it's getting into an infinite loop.


- This
Code:
#/bin/perl

is not going to invoke the perl interpreter. It should try to run those commands under your shell.


- Looking at your input file, how will
Code:
if ($line =~ /$search/)

ever match? How will it ever get to the heart of the code?


- This
Code:
print "@seqArray\n";

may/may not be what you want but it will be empty anyway.

Previous Thread | Next Thread
Test Your Knowledge in Computers #675
Difficulty: Easy
Wi-Fi is generally less vulnerable to hacker attacks than wired networks.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get unique elements from Array

I have an array code and output is below: echo $1 while read -r fline; do echo "%%%%%%$fline%%%%%" fmy_array+=("$fline") done <<< "$1" Output: CR30903 YU0007 SRIL CR30903 Yogesh SRIL %%%%%%CR30903 YU0007 SRIL%%%%% %%%%%%CR30903 Yogesh SRIL%%%%% ... (8 Replies)
Discussion started by: mohtashims
8 Replies

2. UNIX for Beginners Questions & Answers

How To Read a File and Assign the line values to an Array?

i have this basic code that i wrote to read a file and place it's values to an array. the source/input file will have multiple strings on it that is separated by a whitespace. sample_list.txt file contents: ACCT1 TABLE1 ACCT2 TABLE2 ACCT3 TABLE3 script file: sample_list.sh ... (3 Replies)
Discussion started by: wtolentino
3 Replies

3. Shell Programming and Scripting

Count number of unique values in each column of array

What is an efficient way of counting the number of unique values in a 400 column by 1000 row array and outputting the counts per column, assuming the unique values in the array are: A, B, C, D In other words the output should look like: Value COL1 COL2 COL3 A 50 51 52... (16 Replies)
Discussion started by: Geneanalyst
16 Replies

4. Shell Programming and Scripting

How to read values and store in array?

I am reading a value from a file and want to store the value in a dynamic array as i don't know the number of occurrences of the value in that file. How can i do that and then later fetch that value from array (25 Replies)
Discussion started by: Prachi Gupta
25 Replies

5. Shell Programming and Scripting

unique inside array

I have a file root@server # cat /root/list12 11.22.33.44 22.33.44.55 33.44.55.66 33.44.55.66 33.44.55.66 I try to pass to array and display unique. root@server# cat /root/test12.sh #!/bin/bash #delcare array badips and accumulate values to array elemenrs badips=( $( cat... (4 Replies)
Discussion started by: anil510
4 Replies

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

7. Shell Programming and Scripting

Read variables names from array and assign the values

Hi, I have requirement to assign values to variables which are created dynamically. Below is the code which i am using to achieve above requirement. #!/bin/ksh oIFS="$IFS"; IFS=',' STR_FAIL_PARENT_IF_FAILS="WF_F_P_IF_FAILS1,WF_F_P_IF_FAILS2,WF_F_P_IF_FAILS3" set -A... (1 Reply)
Discussion started by: tmalik79
1 Replies

8. Shell Programming and Scripting

PHP: Search Multi-Dimensional(nested) array and export values of currenly worked on array.

Hi All, I'm writing a nagios check that will see if our ldap servers are in sync... I got the status data into a nested array, I would like to search key of each array and if "OK" is NOT present, echo other key=>values in the current array to a variable so...eg...let take the single array... (1 Reply)
Discussion started by: zeekblack
1 Replies

9. Shell Programming and Scripting

Read textfile and enter the values in array

Hi, I want to put values in .txt file into array. Example : $vi repo.txt abc def ghi jkl mno pqr i want the output to be like this: $echo ${mydf} abc $echo ${mydf} def $echo ${mydf} ghi (3 Replies)
Discussion started by: luna_soleil
3 Replies

10. Shell Programming and Scripting

Getting Unique values in a file

Hi, I have a file like this: Some_String_Here 123 123 123 321 321 321 3432 3221 557 886 321 321 I would like to find only the unique values in the files and get the following output: Some_String_Here 123 321 3432 3221 557 886 I am trying to get this done using awk. Can someone please... (5 Replies)
Discussion started by: Legend986
5 Replies

Featured Tech Videos