Visit Our UNIX and Linux User Community


search of string from an array in Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search of string from an array in Perl
# 1  
Old 07-16-2009
search of string from an array in Perl

Hi All

I want to search a string from an array in Perl. If a match occurs, assign that string to a variable else assign 'No match'. I tried writing the script as follows but it's in vain. Please help me..


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

    my $NER;
    my @text=("ORG","PER");

   my @NES=("I-PER","I-TIM","LOC","MISC","ORG","PER");

    for(my $k=1; $k<=29; $k++)
    {
        if ($text[0] eq $NES[k])
        {
                $NER = $text[0];
        }
        else
        {
                $NER="No Match";
        }
    }
  print $NER;



Expected Output:-

ORG


Thanks in advance.

Last edited by Yogesh Sawant; 07-19-2009 at 08:03 AM.. Reason: added code tags
# 2  
Old 07-16-2009
Not the way I would do it but here is your code corrected:

Code:
#!/usr/bin/perl

use strict;

my $NER;
my @text=("ORG","PER");

my @NES=("I-PER","I-TIM","LOC","MISC","ORG","PER");

for(my $k=1; $k<=29; $k++)
{
if ($text[0] eq $NES[$k])
{
$NER = $text[0];
last;
}
else
{
$NER="No Match";
}
}
print $NER;

# 3  
Old 07-17-2009
Thanks a lot. It worked. But can you please write in the way you would like to?
# 4  
Old 07-17-2009
Not knowing anything else, going by only the data you posted and the desired output:

Code:
#!/usr/bin/perl

use strict;
use warnings;

my $NER = 'No Match';
my @text = qw(ORG PER);
my @NES  = qw(I-PER I-TIM LOC MISC ORG PER);

MAINLOOP: for my $text (@text){
   for my $nes (@NES){
      if ($text eq $nes) {
         $NER = $text;
         last MAINLOOP;
      }
   }
}
print $NER;

# 5  
Old 07-19-2009
Thanks a lot.

Previous Thread | Next Thread
Test Your Knowledge in Computers #858
Difficulty: Medium
AJAX is a set of web development techniques using many web technologies on the server-side to create asynchronous web applications.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Search and replace a array values in perl

Hi, i want to search and replace array values by using perl perl -pi -e "s/${d$i]}/${b$j]}" *.xml i am using while loop for the same. if i excute this,it shows "Substitution replacement not terminated at -e line 1.". please tell me what's wrong this line (1 Reply)
Discussion started by: arindam guha
1 Replies

2. Shell Programming and Scripting

Perl - use search keywords from array and search a file and print 3rd field when matched

Hi , I have been trying to write a perl script to do this job. But i am not able to achieve the desired result. Below is my code. my $current_value=12345; my @users=("bob","ben","tom","harry"); open DBLIST,"<","/var/tmp/DBinfo"; my @input = <DBLIST>; foreach (@users) { my... (11 Replies)
Discussion started by: chidori
11 Replies

3. Shell Programming and Scripting

Perl string formation from array

HI I ma using perl programming my perl is like this $InputFile = $ENV{UDE_TMP} . "/" ."cre_fmr_gen.temp_data_file_gen.dat"; @duplicates = `cat $InputFile | cut -d "|" -f 1,1 | sort | uniq -c | awk '{ if(\$1>1) {print \$2;}}'`; my $cusiplist ; foreach $cusip (@duplicates) {... (1 Reply)
Discussion started by: ptappeta
1 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

Search Files from Array and link to original location in Perl

Hello, Question is related to Perl: I need to search few of the files from the array of file names. And after grepping the file names from an array I need to link these files to original location. The original location in this case is ref_path as input from the user. ##$ref_path is... (3 Replies)
Discussion started by: aarora1
3 Replies

6. Shell Programming and Scripting

[Perl] Sorting an String-Array

Hi, i have a txtfile with the format <Nr>tab<word>tab<other stuff>new line and i want to sort the <word>-colum with a perl script. My textfile: <Nr>tab<word>tab<other stuff>new line 6807 die ART.Acc.Sg.Fem 6426 der ART.Gen.Sg.Fem 2 die ART.Nom.Sg.Fem 87 auf APPR.-- 486 nicht PTKNEG.--... (1 Reply)
Discussion started by: buckelede
1 Replies

7. Shell Programming and Scripting

Perl and string array problem

#!/usr/bin/perl my @arr=("hello", "how", "are", "you"); $l=length(@arr); print $l; This print 1.Why? How can i print the array size = 4? I want to store these in an array. hello how are you And then i want to access these element through indexing. How can i do this? (4 Replies)
Discussion started by: cola
4 Replies

8. Shell Programming and Scripting

Perl : Search for next occurence of a word in an array

I have an array as follows: Space: ABC Name: def Age: 22 Type: new Name: fgh Age: 34 Type: old Space: XYZ Name: pqr Age: 44 Type: new : : How can I separate the array with elements starting from Space:ABC until Space: XYZ & put them in a different array & so on... (4 Replies)
Discussion started by: deo_kaustubh
4 Replies

9. Shell Programming and Scripting

Search array elements as file for a matching string

I would like to find a list of files in a directory less than 2 days old and put them into an array variable. And then search for each file in the array for a matching string say "Return-code= 0". If it matches, then display the array element with a message as "OK". Your help will be greatly... (1 Reply)
Discussion started by: mkbaral
1 Replies

10. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

Featured Tech Videos