Creating a vlookup style code
Moderator: General Moderators
Creating a vlookup style code
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
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
and where is your problem?
reading up on http://www.php.net/fgetcsv and related stuff should get you going...
reading up on http://www.php.net/fgetcsv and related stuff should get you going...
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.
$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.
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
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);
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);
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
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
can be shortened to this
PS: When Posting code in Forum please use code tags
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; }Code: Select all
list($bpaCSR,,,,,,$bpaBSCPA) = split(" ", $cline);
if($bpaCSR==$CeID) { $BSCPA=$bpaBSCPA; }- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
just put it before this line but after the curly brace of while
Code: Select all
fwrite($fwop,$CaID.' '.$CeID.' '.$CHwR.' '.$CN ...