View Full Version : Creating multi files from 1 tab-delimited file
StealthFD
10-19-2008, 04:28 PM
Hey guys, long time no chat...
I am kinda stuck on this one and looking for some guidance.
I have a tab-delimited text file. I would like to take each line and
output the data in that line to a new, separate html (php) file. So essentially, I am
exporting a flatfile text database to html pages with each page being
one formatted database record.
I would like to load a text file as a template, then using the tab-delimited file, generate one file per row where each column contains the data from the file.
The file would be saved as one of the data fields.
For example:
ITEM_NUM<TAB>TITLE<TAB>DESCRIPTION
1<TAB>Title 1<TAB>Description 1
2<TAB>Title 2<TAB>Description 2
3<TAB>Title 3<TAB>Description 3
Would generate 3 files.. a 1.php a 2.php and a 3.php each one containing:
<html>
<head>
<title>$TITLE</title>
<meta name="description" content="$DESCRIPTION" />
...etc
I hope that makes sense. Any ideas, suggestions or samples?
Thanks guys!
yosik
10-19-2008, 06:16 PM
The way to go about that would be to use the TextFile.ReadToTable action, then for each element of the Table, use the String.Find String.Left, String.Mid and String.Right actions to separate each TAB separated parameter.
Then insert each Data into place by replacing $TITLE and $DESCRIPTION and saving that string as a file.
Hope that helped pointing you in the right direction.
Good luck
Yossi
StealthFD
10-19-2008, 08:54 PM
Thanks for the help yosik!
I am well on my way and I have everything figured out... but I am still fighting with the String.Find String.Left, String.Mid and String.Right actions to break out the elements into variables so that I can write them out to the files correctly.
I converted the file to comma delimited but I am still struggling. Could you please show me an example of the best way to break out these fields into variables from a text string?:
sampleitem1,sam_item2,sample3,sam4,sample5,item6
I can't belive that I am having such a difficult time with such a simple task. I know there has to be a simpler way than what I am trying to do but my mind is numb... I think I just need a KICK in the right direction.
Thanks so much!
Ulrich
10-19-2008, 09:45 PM
There is a function shipped with AMS which you can use. Look for DelimitedStringFunctions.lua in the Gallery\Scripts subfolder of your AMS installation. With this function you will be able to split strings separated with TAB, comma, etc. immediately. Just copy and paste the code into your project...
Ulrich
yosik
10-20-2008, 01:17 AM
Right Ulrich. I forgot about this one, but that is exactly what StealthFD needs.
Thanks for reminding us.
Yossi
StealthFD
10-20-2008, 07:32 AM
I'll give that a try... Thanks guys!
StealthFD
10-20-2008, 08:49 AM
I can't belive that I am having so many problems with this...
I can't even get the DelimitedStringFunctions.lua function and the sample script to work in a new project.
Can someone else please try the DelimitedStringFunctions.lua function and the sample script and let me know that I'm not crazy... well, at least not about this.
Ulrich
10-20-2008, 11:25 AM
You aren't crazy, the example script won't work correctly as it is written, because the variable used in the sample ("string") is a reserved word in Lua.
If you just rename the variable to something else, like TestString, and the script will work. Place the DelimitedStringToTable, TableToDelimitedString etc. functions in the Global Functions tab, and try this:
TestString = "Brett,Mark,Darryl,Lorne,Adam";
tbResults = DelimitedStringToTable(TestString, ",");
for i, name in tbResults do
Dialog.Message("Name", name);
end
Ulrich
StealthFD
10-20-2008, 11:32 AM
OMG, can't belive I missed that.
Thank you, you just confirmed that I am way overdue for a vacation!
StealthFD
10-20-2008, 10:26 PM
I just wanted to thank you guys for your help. I have completed the project and am just making it look pretty now.
I couldn't get the DelimitedStringToTable function to work with the TAB delimiter... so I just resaved the file as comma delimited.
Just for future reference, how do you set the DelimitedStringToTable delimiter to TAB or Char(9) ???
I tried this with no luck:
tcharacter = String.Char(9);
for a,b in items do
TestString = b;
tbResults = DelimitedStringToTable(TestString, tcharacter);
end
I'm working great - I am just curious. Thanks again! You guys are always great help!
.
Ulrich
10-20-2008, 11:18 PM
Just for future reference, how do you set the DelimitedStringToTable delimiter to TAB or Char(9) ???
To split fields separated by tabs, this is the easiest way:
tbResults = DelimitedStringToTable(TestString, "\t");
Ulrich
StealthFD
10-21-2008, 05:06 PM
Thanks for the help!
Powered by vBulletin™ Version 4.0.6 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.