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: Intermediate:
How to put the data of two file into the third file in a different fashion

 



mail4deveshg
Novice

Jul 13, 2011, 2:40 AM

Post #1 of 9 (2139 views)
How to put the data of two file into the third file in a different fashion Can't Post

Hi All,

Problem Statement: I have two files and they contain the comma separated values. I have to read these files and put selected values into the third file. for example: I have to put 1234 and 363 from A.txt & B.txt into the third file C.txt. Row should match in this case, such as first row of A.txt and first row of B.txt. and so on.

A.txt

1234,652,2726,373

2526,363,3536,353

B.txt

363,4747,5757

464,4748,3639

What I have done: I have already read these files and put it into an array. But I am not able to print the values in the above fashion .e.g. [1234, 363].

In the C.txt file I have to put the value like this

1234,363

2526,464



Could anyone help me with this?



Thanks,

Devesh


BillKSmith
Veteran

Jul 13, 2011, 4:22 AM

Post #2 of 9 (2135 views)
Re: [mail4deveshg] How to put the data of two file into the third file in a different fashion [In reply to] Can't Post

Process the files line-by-line. Do not create arrays.

Use split to parse the simple comma separated files.


Code
  

use strict;
use warnings;
open my $A_IN, '<', 'a.txt' or die "Cannot open a.txt\n";
open my $B_IN, '<', 'b.txt' or die "Cannot open b.txt\n";
open my $C_OUT, '>', 'c.txt' or die "Cannot open c.txt\n";
while (1) {
my $a_line = <$A_IN>;
my $b_line = <$B_IN>;
last if !$a_line and !$b_line;
die "Input files are not the same length\n" if $a_line xor $b_line;
chomp $a_line;
chomp $b_line;
my $field_a0 = (split /,/, $a_line)[0];
my $field_b0 = (split /,/, $b_line)[0];
print {$C_OUT} "$field_a0,$field_b0\n";
}
close $C_OUT;
close $B_IN;
close $A_IN;

Good Luck,
Bill


mail4deveshg
Novice

Jul 13, 2011, 4:45 AM

Post #3 of 9 (2133 views)
Re: [BillKSmith] How to put the data of two file into the third file in a different fashion [In reply to] Can't Post

Hi Bill,

Thanks a lot for your help.

Could you please let me know to do this task using array. Because, before printing the values into the files I have to do some validation.



Thanks,

Devesh


BillKSmith
Veteran

Jul 13, 2011, 6:02 AM

Post #4 of 9 (2130 views)
Re: [mail4deveshg] How to put the data of two file into the third file in a different fashion [In reply to] Can't Post

There are many ways you could save the data. Trade-offs between processing speed, memory requirements, and development time depend on how you use the data. Please tell us, at least, what data you need for "validation".
Good Luck,
Bill


mail4deveshg
Novice

Jul 13, 2011, 10:07 PM

Post #5 of 9 (2110 views)
Re: [BillKSmith] How to put the data of two file into the third file in a different fashion [In reply to] Can't Post

Hi Bill,



Thanks for your reply.

Actually I have three files. Here below are the formats of all these files.

A.txt
00927
00923
00937

B.txt
00927,14/07/2011,200
00921,14/07/2011,100

C.txt
00876
00826
00716


My Problem:

First I have to search the 00927 (from the A.txt file) in the B.txt file. If it exists I have to update that row with the today date and the next number (200, it is some standard value) in the B.txt file. Then I have to prepare a command that include the 00876 from the C.txt file and write it to next file D.txt.



If it not exists in the B.txt file then I have to take the value 00937 form the A.txt file and update the B.txt files.
00937,14/07/2011,100 // like that


Command format: (This command I have to write in the D.txt file)

../bin/abcFor/ -t 00927 r 00876

../bin/abcFor/ -t 00923 r 00826

../bin/abcFor/ -t 00937 r 00716



Hope this helps.


Thanks,
Devesh


(This post was edited by mail4deveshg on Jul 13, 2011, 10:11 PM)


mail4deveshg
Novice

Jul 14, 2011, 5:23 AM

Post #6 of 9 (2069 views)
Re: [BillKSmith] How to put the data of two file into the third file in a different fashion [In reply to] Can't Post

Hi Bill, All

Did you get a chance to look at this problem.

Appriciate any help on this.



Thanks,

Devesh


BillKSmith
Veteran

Jul 14, 2011, 9:10 AM

Post #7 of 9 (2043 views)
Re: [mail4deveshg] How to put the data of two file into the third file in a different fashion [In reply to] Can't Post

Only the data from B.txt should be saved.


Code
my @b_data; 
open my $B_IN, '<', 'B.txt' or die "Cannot open B.txt\n";
while (<$B_IN>){
push @b_data, [split /,/, $_];
}
close $B_IN;



Update it inside the A-file loop.


Code
    foreach (@b_data) { 
if ($_->[0] == $a_line) {
$_->[1] = $TODAY;
}
}

Good Luck,
Bill


mail4deveshg
Novice

Jul 14, 2011, 11:33 PM

Post #8 of 9 (1959 views)
Re: [BillKSmith] How to put the data of two file into the third file in a different fashion [In reply to] Can't Post

Hi Bill,

Could you please send me the complete code.

it will be a great help. I need it urgently.

I am also putting my code for your ref.

#!/usr/bin/perl

open(FILE1, "broker.txt") or die("Unable to open file");
@BRdata = <FILE1>;
close(FILE1);

open(FILE2, "sequence.txt") or die("Unable to open file");
@SEQdata = <FILE2>;
#close(FILE2);

open(FILE3, "im.txt") or die("Unable to open file");
@IMdata = <FILE3>;
close(FILE3);

open $IM, '<', 'im.txt' or die "Unable to open file\n";

open Myfile, ">temp2.txt";

foreach $file1 (@BRdata)
{
$t1 = $file1;
$t1_0 = $t1;
chomp($t1_0);

$t3_0 = <$IM>;
chomp $t3_0;

$bFlag = false;

foreach $file2 (@SEQdata)
{
$t2 = $file2;

$t2_0 = (split /,/, $t2)[0];
chomp($t2_0);chomp($t3_0);

if($t1_0 == $t2_0)
{
$bFlag = true;
print Myfile "$t1_0 $t2_0 $t3_0\n";
}

if($bFlag = false)
{
print "true value\n";
print "$t1_0\n";
}

}

}



-------

this code is working fine for the 00927 & 00923 from the A.txt file. but I need, if the value is 00937 then i want to update the B.txt file

00937,14/07/2011,200



could you please help me with this. Or please send me the complete code, it will be a great help for me.



Thanks,

Devesh


BillKSmith
Veteran

Jul 15, 2011, 7:45 AM

Post #9 of 9 (1953 views)
Re: [mail4deveshg] How to put the data of two file into the third file in a different fashion [In reply to] Can't Post


Quote
Could you please send me the complete code.

it will be a great help. I need it urgently.



This is not the purpose of this forum and no one here is on your schedule.

The code you posted ignores my advice without explanation. I am not sure that I could match the file names in your code with the file names in your text. The code contains nothing about commands or dates. When your code and text disagree, which should we believe? It is probably impossible to write production code without a much more accurate and detailed specification.
Good Luck,
Bill

 
 


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

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