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:
query a file, search another and output a third

 



dsaliba
Novice

May 29, 2013, 2:50 AM

Post #1 of 5 (456 views)
query a file, search another and output a third Can't Post

file1.txt
1
3
5

file2.txt
1 12378
2 98979
3 57778
4 87897
5 78979
6 29787

file3.txt
1 12378
5 78979

My issue is that I'm not getting all the outputs. Cannot understand what could be the issue. Would someone kindly point out why? Thank you.


Code
#! /usr/bin/perl 

open my $result,'>','file3.txt' or die $!;
open my $imsi,"<",'file2.txt' or die $!;
my %hash = map {split/\s+/,$_,2} <$imsi>;
open my $msisdn,'<','file1.txt' or die $!;
while($line=<$msisdn>){
chomp ($line);
if (exists $hash{$line}){
print $result "$line: $hash{$line}";
}
}



(This post was edited by dsaliba on May 29, 2013, 2:53 AM)


BillKSmith
Veteran

May 29, 2013, 6:11 AM

Post #2 of 5 (436 views)
Re: [dsaliba] query a file, search another and output a third [In reply to] Can't Post

Your code works fine for me. You probably have whitespace or non-printing characters in file1 or file2.

Fix the data file to verify the problelm. You may want to change your program to handle the "bad" data.
Good Luck,
Bill


dsaliba
Novice

May 29, 2013, 6:40 AM

Post #3 of 5 (431 views)
Re: [BillKSmith] query a file, search another and output a third [In reply to] Can't Post

Yes that issue came to mind but I have no idea to go about it. Any pointers please?


Laurent_R
Veteran / Moderator

May 29, 2013, 10:50 AM

Post #4 of 5 (407 views)
Re: [dsaliba] query a file, search another and output a third [In reply to] Can't Post

Please provide your test files.

One possible issue is that you prepare your file under Windows and run them on *nix, or vice-versa. You would get end of line characters problem, and chomp is not sufficient in this case.

If this is the case add the following line after chomp:


Code
$line =~ s/\r//g;


The other thing puzzling me is that you don't chomp what you put into your hash.

BTW, imsi, msisdn... it looks like you and I are working in the same industry. Wink


(This post was edited by Laurent_R on May 29, 2013, 2:17 PM)


hwnd
User

May 29, 2013, 11:36 AM

Post #5 of 5 (403 views)
Re: [Laurent_R] query a file, search another and output a third [In reply to] Can't Post

Why not do something like this?


Code
open my $fh1, '<', 'file1.txt' or die "failed: $!"; 
open my $fh2, '<', 'file2.txt' or die "failed: $!";
open my $out, '>', 'file3.txt' or die "failed: $!";

chomp(my @lines = <$fh1>);

while (<$fh2>) {
chomp;
my ($k, $v) = split;
if ( grep { $k == $_ } @lines ) {
print $out $_;
}
}

close $fh1;
close $fh2;
close $out;

__OUTPUT__
1 12378
3 57778
5 78979


 
 


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

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