CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Finding a string from one file into another file

 



saurabhsmahajan
New User

Dec 3, 2013, 3:14 PM

Post #1 of 6 (1583 views)
Finding a string from one file into another file Can't Post

Hello Guys,

I need help in searching for a string from one file into another file and printing it.I have two files "id.txt" and "path.txt".
id.txt has values as below
X34256
Y64375
Y32435
Y89756
X85263
and path.txt has values as below
X34256_BULB_STR
X34256_BUB_STR
X34256_BULB_STR2
Y89756_ROD_INT
Y89756_ROD_INT2

Now here I want to find all the values from file path.txt which would start with the value from id.txt

Here is my program as below. But would print only one line and not iterating through the complete file.

#use strict;
use warnings;

open DFH,'<',"D:\\perlprog\\id.txt" or die "Could not open file";
open VFH,'<',"D:\\perlprog\\path.txt" or die "Could not open file";
foreach (my $da=<DFH>)
{
chomp $da;
print $da;
while (<VFH>)
{
print $_ if(/$da/);

}
}

Could you please help me solve this issue.
Apologize if commiting any mistake as I am beginner.

Thanks.


Kenosis
User

Dec 3, 2013, 7:34 PM

Post #2 of 6 (1571 views)
Re: [saurabhsmahajan] Finding a string from one file into another file [In reply to] Can't Post


Quote
Now here I want to find all the values from file path.txt which would start with the value from id.txt


Help me understand this specification. You say, "start with the value," but there are multiple values in id.txt.

What is your expected output, given these two files?


saurabhsmahajan
New User

Dec 4, 2013, 1:58 AM

Post #3 of 6 (1557 views)
Re: [Kenosis] Finding a string from one file into another file [In reply to] Can't Post

Thanks Kenosis for the reply.

What I actually want is, if the matching ID from id.txt is found in the path.txt then that complete path.txt line should be printed.


Laurent_R
Veteran / Moderator

Dec 4, 2013, 4:56 AM

Post #4 of 6 (1550 views)
Re: [saurabhsmahajan] Finding a string from one file into another file [In reply to] Can't Post

First read the full id.txt file and store the values into a hash. Then read the path.txt file, check is an entry exists in the hash for the first 6 letters of the file (assuming all the entries in id.txt have a length six characters).


saurabhsmahajan
New User

Dec 4, 2013, 7:30 AM

Post #5 of 6 (1544 views)
Re: [Laurent_R] Finding a string from one file into another file [In reply to] Can't Post

I tried something as below.... BUT NO RESULT.

DFH = file handle for id.txt file

VFH = file handle for path.txt file

foreach my $id(@DFH)
{
print $id;
chomp $id;
{
while(<VFH>)
{
if($_ =~ /$id/)
{
print $_;
}

}
}
}




Will try your solution now


(This post was edited by saurabhsmahajan on Dec 4, 2013, 7:31 AM)


Laurent_R
Veteran / Moderator

Dec 4, 2013, 10:16 AM

Post #6 of 6 (1536 views)
Re: [saurabhsmahajan] Finding a string from one file into another file [In reply to] Can't Post

Yes, try it, it is likely to work better. Don't hesitate to ask if you encounter some trouble.


(This post was edited by Laurent_R on Dec 4, 2013, 10:17 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives