Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing text file and putting the parsed data into database. 1

Status
Not open for further replies.

omerdurdu

Programmer
Joined
Jul 21, 2001
Messages
72
Location
TR
I am trying to parse a text file and then I would like to put them into a database. I have a text file like this

PARKCODE: AKKA
PARKNUM:
SUBJECT: Info systems
VERSION:
KEYWORDS: digital data and text
TITLE: AKKA national monument

and I have a database I created has fields exactly same in the text file.
Like PARKCODE PARKNUM SUBJECT VERSION KEYWORDS TITLE

Can someone tell me how am i gonna parse the data from text file above and put them into database. I would like to parse just data after column like:
not PARKCODE just data after column (:) AKKA.

Thanks for any help.
 
Well.. I HAD the code written out and the tek tips server restarted I guess because all session data was lost...

Here's what you do.. you loop on file output for #chr(10)# as a delimiter, you loop inside that loop on : as a delimiter, put the first item into an aray called ItemArray.. and then every item after that into an array called ValueArray..

You then will be able to say like... #ItemArray[1]#:#ValueArray[1]# and get the needed result..

Grumble.. I hate the Internet...
 
Thanks for you help
Actually I worked on it alot but couldnt figure that out.
Maybe I am making mistakes somewhere
Can you show me that in the code.
I appreciate it.
Thanks
 
<CFFILE action=&quot;read&quot; file=&quot;data.dat&quot; output=&quot;fv&quot;>

<CFSET ItemArray=ArrayNew(1)>
<CFSET ValueArray=ArrayNew(1)>
<CFSET ExcessArray=ArrayNew(1)>

<CFSET counter=1>
<CFLOOP list=&quot;#fv#&quot; index=&quot;datarow&quot; delimiters=&quot;#chr(10)#&quot;>
<CFSET counter=COUNTER+1>
<CFSET incounter=0>
<CFLOOP list=&quot;#datarow#&quot; index=&quot;half&quot; delimiters=&quot;:&quot;>
<CFIF incounter eq 0>
<CFSET ItemArray[counter]=&quot;#half#&quot;>
<CFSET incounter=incounter+1>
<CFELSEIF incounter eq 1>
<CFSET ValueArray[counter]=&quot;#half#&quot;>
<CFELSEIF incounter gt 1>
<CFSET ExcessArray[counter]=&quot;#ValueArray[counter]#&quot;>
<CFSET ValueArray[counter]=&quot;#half#&quot;>
<CFSET ValueArray[counter]=&quot;#ExcessArray[counter]#:#half#&quot;>
</CFIF>
</CFLOOP>
</CFLOOP>
 
Thanks for the code.
can you tell me how am i gonna get output:

i put <cfoutput>#ValueArray[1]#</cfoutput>
it didnt work.
Am I gonna use something else for that.
Thanks for help
 
When I put the output like this
it is giving error

<cfoutput>#ValueArray[counter]#:#half#</cfoutput>

An error occurred while evaluating the expression:
#ValueArray[counter]#

What can be wrong
Thanks

 
Slight glitch.. won't hurt anything through.. try #ValueArray[2]#... It should work fine then.
 
I tried like this
and it didnt work again:
<cfoutput>#ValueArray[2]#:#half#</cfoutput>
<cfoutput>#ValueArray[3]#:#half#</cfoutput>
<cfoutput>#ValueArray[4]#:#half#</cfoutput>
.
.
.

it is giving error like this:
An error occurred while evaluating the expression:
#ValueArray[5]#
Error near line 28, column 24.
The element at position 5 in dimension 1 of object &quot;ValueArray&quot; cannot be found. That dimension of the object is empty. Please, modify the index expression.
The error occurred while processing an element with a general identifier of (#ValueArray[5]#), occupying document position (28:13) to (28:27).


 
try this for getting the results

<CFOUTPUT><CFLOOP from=2 to=&quot;#counter#&quot; index=&quot;thiscount&quot;>
#ItemArray[thiscount]#:#ValueArray[thiscount]#
</CFLOOP></CFOUTPUT>

If it still trips on the last one try..

<CFOUTPUT><CFLOOP from=2 to=&quot;#evaluate(counter-1)#&quot; index=&quot;thiscount&quot;>
#ItemArray[thiscount]#:#ValueArray[thiscount]#
</CFLOOP></CFOUTPUT>
 
Thank you so much. I learned a lot new stuff.
I really needed the result for the ValueArray(thiscount)
and I got it like this
AKKA Info systems digital data and text

Now, I would like to inset them into my database but the problem is I cant get the output as follows.
ValueArray(1)= AKKA
ValueArray(2)= Info System
ValueArray (3) = digital data and text.

If I get the results above I can write a CFQUERY and insert them into my database.
Do you have any idea to get out like
<cfoutput>#ValueArray(1)#</cfoutput>
<cfoutput>#ValueArray(2)#</cfoutput>
.
.

Thanks for your help

 
To start the count at 1.. use the following code, should work fine...

<CFFILE action=&quot;read&quot; file=&quot;data.dat&quot; output=&quot;fv&quot;>

<CFSET ItemArray=ArrayNew(1)>
<CFSET ValueArray=ArrayNew(1)>
<CFSET ExcessArray=ArrayNew(1)>

<CFSET counter=0>
<CFLOOP list=&quot;#fv#&quot; index=&quot;datarow&quot; delimiters=&quot;#chr(10)#&quot;>
<CFSET counter=COUNTER+1>
<CFSET incounter=0>
<CFLOOP list=&quot;#datarow#&quot; index=&quot;half&quot; delimiters=&quot;:&quot;>
<CFIF incounter eq 0>
<CFSET ItemArray[counter]=&quot;#half#&quot;>
<CFSET incounter=incounter+1>
<CFELSEIF incounter eq 1>
<CFSET ValueArray[counter]=&quot;#half#&quot;>
<CFELSEIF incounter gt 1>
<CFSET ExcessArray[counter]=&quot;#ValueArray[counter]#&quot;>
<CFSET ValueArray[counter]=&quot;#half#&quot;>
<CFSET ValueArray[counter]=&quot;#ExcessArray[counter]#:#half#&quot;>
</CFIF>
</CFLOOP>
</CFLOOP>

<CFOUTPUT><CFLOOP from=1 to=&quot;#counter#&quot; index=&quot;thiscount&quot;>
#ItemArray[thiscount]#:#ValueArray[thiscount]#
</CFLOOP></CFOUTPUT>

I'm afraid for further tweaking to get this working perfect we're gonna have to conspire alot more on it.. If you'd like, you can connect me on the following IMers:

AIM: Clickthru Bible
Yahoo: Webmigit or Bibleclicks
MSN: webmigit@hotmail.com
 
Thanks you so much, you are great
 
Hehe... mind clicking the little &quot;Mark this as a helpful or expert post&quot; under my last reply? :)

Hehe.. People always say thanks but never remember to click..
 
I did for your each answer
and i sent an email to you hotmail account
about a question related that if i have many records in a text file. The text file i was working had just one record.
if there are many record and delimiter is &quot;@@&quot;
what am I gonna do. I did CFLOOP but giving strange result.
I sent you my code and a text file.
Thanks for your help
 
Code:
<CFFILE action=&quot;read&quot; file=&quot;data.dat&quot; output=&quot;fv&quot;>

<CFSET ItemArray=ArrayNew(1)>
<CFSET ValueArray=ArrayNew(1)>
<CFSET ExcessArray=ArrayNew(1)>

<CFSET ArrayClear(ItemArray)>
<CFSET ArrayClear(ValueArray)>
<CFSET ArrayClear(ExcessArray)>

<CFSET fv=replace(fv,&quot;#chr(10)##chr(10)#@@#chr(10)#&quot;,&quot;¿&quot;,&quot;ALL&quot;)>

<CFLOOP list=&quot;#fv# index=&quot;group&quot; delimiters=&quot;¿&quot;>
<CFSET ArrayClear(ItemArray)>
<CFSET ArrayClear(ValueArray)>
<CFSET ArrayClear(ExcessArray)>
<CFSET counter=0>
<CFLOOP list=&quot;#group#&quot; index=&quot;datarow&quot; delimiters=&quot;#chr(10)#&quot;>
 <CFSET counter=COUNTER+1>
 <CFSET incounter=0>
 <CFLOOP list=&quot;#datarow#&quot; index=&quot;half&quot; delimiters=&quot;:&quot;>
  <CFIF incounter eq 0>
   <CFSET ItemArray[counter]=&quot;#half#&quot;>
   <CFSET incounter=incounter+1>
  <CFELSEIF incounter eq 1>
   <CFSET ValueArray[counter]=&quot;#half#&quot;>
  <CFELSEIF incounter gt 1>
   <CFSET ExcessArray[counter]=&quot;#ValueArray[counter]#&quot;>
   <CFSET ValueArray[counter]=&quot;#half#&quot;>
   <CFSET ValueArray[counter]=&quot;#ExcessArray[counter]#:#half#&quot;>
  </CFIF>
 </CFLOOP>
</CFLOOP></CFLOOP>

<CFOUTPUT><CFLOOP from=1 to=&quot;#counter#&quot; index=&quot;thiscount&quot;>
#ItemArray[thiscount]#:#ValueArray[thiscount]#<BR>
</CFLOOP></CFOUTPUT><BR><BR>
</CFLOOP>

should do the trick!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top