2

i have two array in my perl and i want to grep one array from other array my code in perl is as follows.

#!/usr/bin/perl  
open (han5, "idstatus.txt");  
open (han4, "routename.txt");  
@array3 = <han4>; 
 @array4 = <han5>;  
foreach (@array3) { 
@out = grep(/$_/, @array4); 
print @out; }

file routename.txt

strvtran
fake
globscr 

file idstatus.txt

strvtran online   
strvtran online  
strvtran online
globscr online 
globscr online
globscr online  
globscr online  
globscr online 
Xtech dead  
Xtech dead  
fake online  
fake online  
fake connecting
walkover123 online  
walkover123 online

Now what i want to grep globscr element from idstatus.txt and output should be like:

globscr online 
globscr online
globscr online  
globscr online  
globscr online

and i dont want to use any system command. please help me here

0

2 Answers 2

3

Instead of grepping each line, consider building a regex that contains the route names as alternations:

use strict;
use warnings;
use autodie;

open my $rnameFH, '<', 'routename.txt';
chomp( my @routename = <$rnameFH> );
close $rnameFH;

my $names = '(?:' . ( join '|', map { "\Q$_\E" } @routename ) . ')';
my $regex = qr /^$names/;

open my $idFH, '<', 'idstatus.txt';

while(<$idFH>){
    print if /$regex/;
}

close $idFH;

Output on your datasets:

strvtran online   
strvtran online  
strvtran online
globscr online 
globscr online
globscr online  
globscr online  
globscr online 
fake online  
fake online  
fake connecting

The script creates an OR-type regex, by joining the route names with "|" (print $names to see this). The map is there only to quote any meta-characters which may be in the names, e.g., .*^, etc., as these would impact the matching.

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

2

You are not removing the newlines, so your match is including a newline in what it is looking for.

You also need to make the for loop use a different variable, since inside the grep, $_ will only refer to the element of the list given to grep currently being inspected.

Try:

chomp(@array3 = <han4>);
@array4 = <han5>;  
foreach my $routename (@array3) { 
    @out = grep(/$routename/, @array4); 
    print @out;
}

This will output:

strvtran online   
strvtran online  
strvtran online
fake online  
fake online  
fake connecting
globscr online 
globscr online
globscr online  
globscr online  
globscr online 

I'm not sure what you mean by wanting to grep globscr from idstatus.txt; what role does routename.txt play then?

2 Comments

file idstatus has multiple element and and some the element exist in routename. eg. in idstatus.txt there is an element "strvtran online" and an element in routename.txt is "strvtran". so i want to grep "strvtran" from idstatus.txt and get the element of idstatus.txt like "strvtran online"...... i have used your script but it is not displaying anything......
then you didn't use my script or your files are not what you say? show exactly what you did run (edit your question)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.