Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
Python, Lua, Tcl, C and C++ training - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, Ruby, MySQL and Linux / Tomcat systems
 
how to join two sets of data?

Posted by yamaha102 (yamaha102), 29 January 2008
how to join the FileA and FileB to become finalfile?
Please guide. thanks

a.txt:
Product,Auto,Manual
NF,0,431
NT,0,95

b.txt:
Product,M+U
NT,65
NF,156

FinalFile:
Product,M+U,Auto,Manual
NT,65,0,431
NF,156,0,95


_____
Code:
______
use strict;
use warnings;

open (INPUT1,"a.txt") or die "Cannot open the file: $!";
open (INPUT2,"b.txt") or die "Cannot open the file: $!";

while (my $line1 = <INPUT1>) {
my ($Product, $Auto, $Manual) = split(/,/, $line1);


while (my $line2 = <INPUT2>)  {
my ($Product1, $MU) = split(/,/, $line2);
print "$Product1,$MU, $Auto, $Manual\n" if($Product=$Product1);

}
}



close INPUT1;
close INPUT2;


Posted by Custard (Custard), 29 January 2008
Hi,

I haven't time for a full solution, so I'll nudge you in the right direction.

Hashes are your friend (in this case)

Something like (untested pseudocode)

Code:
#Read data for a
my ( $product, @data_a ) = split ....

# This is a 'hash slice' that puts all of @data into %data's keys
@data{ @data_a } = undef;

# Read data for b
my ($product, @data_b ) = split...

# Store data for b into the data hash keys
@data{ @data_b } = undef;

# Store reference to data hash
$products{ $product } = \%data;

# Write out result (for product only)

foreach my $product (keys %products) {
   print( "$product: ".join(',',  keys %{$products{ $product }})."\n");
}



Your task should you choose to accept it, is to adapt this to work for your other data columns.

hth

B

Posted by KevinAD (KevinAD), 29 January 2008
verify is the final output is right or wrong:

FinalFile:
Product,M+U,Auto,Manual
NT,65,0,431
NF,156,0,95

looks wrong to me judging by the input files. 431 and 95 look transposed to me.

Posted by Custard (Custard), 29 January 2008
I hadn't noticed that.

Its hard to tell sometimes whether people just want the answer, for homework say or need a prod in the right direction.

I'm not sure the above is exactly the best way maybe (TMTOWTDI), but it might encourage a bit of thought and reading up on hashes and stuff.

Something like this problem may even be in the perl FAQ somewhere.

b

Posted by KevinAD (KevinAD), 29 January 2008
on 01/29/08 at 21:47:36, Custard wrote:
I hadn't noticed that.

Its hard to tell sometimes whether people just want the answer, for homework say or need a prod in the right direction.

I'm not sure the above is exactly the best way maybe (TMTOWTDI), but it might encourage a bit of thought and reading up on hashes and stuff.

Something like this problem may even be in the perl FAQ somewhere.

b



Yes, it is hard to tell. But at least this person has posted some code.  Hell, they even used strict and warnings. If the first field is unique, a hash (of arrays) is definetly the way to go. Hopefully the OP understands references.

untested code:

use strict;
use warnings;

my %hash = ();

open (INPUT,"b.txt") or die "Cannot open the file: $!";  
while (my $line = <INPUT>) {  
  chomp;
  my ($Product,$Mu) = split(/,/, $line);
  push @{$hash{$Product}},$Mu;
}
close INPUT;
open (INPUT,"a.txt") or die "Cannot open the file: $!";  
while (my $line = <INPUT>)  {  
  my ($Product, $Auto, $Manual) = split(/,/, $line);
  push @{$hash{$Product}},$Auto, $Manual;
}  
close INPUT;
foreach my $p (keys %hash) {
   print join(',',$p,@{$hash{$p}}),"\n";
}        

Posted by yamaha102 (yamaha102), 30 January 2008
Thanks.
May I know what does below statement doing? I'm confused about the @{$hash{$Product}}
push @{$hash{$Product}},$MU;

Posted by Custard (Custard), 30 January 2008
Hi,

Its an array reference stored in a hash.

You're probably used to

push @array, $element;

and $hash{ item } = $item;

Well

the $hash{ $Product } contains a reference to an array.

To dereference that array we put @{ ... } around the reference.

So to get the array back you could say

@array = @{ $hash{ $Product } };

hth.

Posted by KevinAD (KevinAD), 30 January 2008
Ijn addition to Custards explanation, the first three tutorials on this page http://perldoc.perl.org/index-tutorials.html discuss references (or complex data). The second one is especially in depth.  



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2017: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho