Page 1 of 1
Creating a vlookup style code
Posted: Wed Feb 09, 2005 4:15 am
by mattiep
Hi,
I have 2 TAB delimited text files, both have around 1000 lines, they come from different sources, but both have a common identifier.
What I want to do is merge these two files together. And save them as CSV or TAB delimited (it doesnt matter). In one of the lines of the data I will need to have all 5 fields from the first text file, followed by one of the fields from the second text file. I will then need to fwrite() this to another file.
TIA.
Mattie P
Posted: Wed Feb 09, 2005 4:20 am
by timvw
and where is your problem?
reading up on
http://www.php.net/fgetcsv and related stuff should get you going...
Posted: Wed Feb 09, 2005 4:41 am
by mattiep
I can read the first file and export that into an output file, using the following:
$fgv = fopen($mylistfile, "r");
$vlinecounter=0;
while($vline = fgets($fgv,4096)) {
if($vlinecounter++==0) {
continue;
}
list($A,$B,$C) = split(" ", $vline);
But I am unable to read the second file & combine using this method.
Posted: Wed Feb 09, 2005 4:44 am
by n00b Saibot
you can simply use file() to read in contents of file as array thereafter do as you wish with the contents. selct, copy, write somewhere else etc.
Posted: Wed Feb 09, 2005 4:53 am
by timvw
in each loop you should not only rread from file1, but also from file2..
can't see the difficulty
Posted: Wed Feb 09, 2005 5:06 am
by mattiep
How do I read the second file in the same loop? I tried to create a second loop under the first loop, but this caused the page to crash.
Posted: Wed Feb 09, 2005 8:38 am
by feyd
sounds like you second loop was either an infinite loop, or an exponential i.e. inside the original loop.
Posted: Wed Feb 09, 2005 9:04 am
by mattiep
OK, I am pretty new to PHP, so all of this is probably very helpful, but could you simplify it for me??
Ta
Posted: Wed Feb 09, 2005 9:20 am
by feyd
post your new code that "crashes"
Posted: Wed Feb 09, 2005 9:28 am
by mattiep
Hi Feyd,
Here is the code:
<?php
$fgv = fopen($mylistfile, "r");
$vlinecounter=0;
while($vline = fgets($fgv,4096)) {
if($vlinecounter++==0) {
continue;
}
list($CaID,$CeID,$CHwR,$CN,$DD,$A,$Gen,$D,$P,$SA,$TC,$OC,$FD,$FN,$RT,$AT,$IH,$LN,$DE,$St,$Geo,$BR,$M,$SLA,$EOL) = split(" ", $vline);
$BSCPA='';
$fgc=fopen($myotherfile, "r");
$clinecounter=0;
while($cline = fgets($fgc,4096)) {
if($clinecounter++==0) {
continue;
}
list($bpaCSR,$bpaSN,$bpaSSF1,$bpaSSF19,$bpaSSF20,$bpaBSCid,$bpaBSCPA) = split(" ", $cline);
if($bpaCSR==$CeID) { $BSCPA=$bpaBSCPA; }
}
fwrite($fwop,$CaID.' '.$CeID.' '.$CHwR.' '.$CN.' '.$DD.' '.$A.' '.$Gen.' '.$D.' '.$P.' '.$SA.' '.$TC.' '.$OC.' '.$FD.' '.$FN.' '.$RT.' '.$AT.' '.$IH.' '.$LN.' '.$DE.' '.$St.' '.$Geo.' '.$BR.' '.$M.' '.$SLA.' '.$BSCPA."\n");
}
fclose($fgc);
fclose($fgv);
fclose($fwop);
Posted: Wed Feb 09, 2005 9:49 am
by n00b Saibot
good god! that's sure to crash and screw your system.
that's crashin' because you are opening second file inside first loop when first file is also open and not closing it inside first loop itself. You are closing it outside of the loop. Just close it inside of the first loop.
and second thing is this
Code: Select all
list($bpaCSR,$bpaSN,$bpaSSF1,$bpaSSF19,$bpaSSF20,$bpaBSCid,$bpaBSCPA) = split(" ", $cline);
if($bpaCSR==$CeID) { $BSCPA=$bpaBSCPA; }
can be shortened to this
Code: Select all
list($bpaCSR,,,,,,$bpaBSCPA) = split(" ", $cline);
if($bpaCSR==$CeID) { $BSCPA=$bpaBSCPA; }
PS: When Posting code in Forum please use
code tags
Posted: Wed Feb 09, 2005 1:10 pm
by mattiep
Where abouts in the loop should I close the file? I closed it after the list... part, and the script errored
Posted: Thu Feb 10, 2005 12:12 am
by n00b Saibot
just put it before this line but after the curly brace of while
Code: Select all
fwrite($fwop,$CaID.' '.$CeID.' '.$CHwR.' '.$CN ...