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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing to text files with variables

Status
Not open for further replies.

impulse24

IS-IT--Management
Jul 13, 2001
167
US
Hi All,

I tried posting this once before with no luck. Can someone please help me with this.

I get fixed width text files, with data looking like below.

Spiderman 4.50 Marvel
Superman 3.00
2.00 DC
Batman DC

I put the above values into three different variables
strHero
strPrice
strPublisher

I then want to print these values back to a new text file. I want to keep each line in the original file, on the same line in the new file. But if a variable is blank I don't want to print it. Any idea how to do this
Below is my output:
Hero=Spiderman,Price=4.50,Publisher=Marvel
Hero=Superman,Price=3.00
Price=2.00,Publisher=DC
Hero=Batman,Publisher=DC

As you can see from above, variables that are empty in the first file are not printed to my new file. That's where I am lost.


 
In your original text file what separates the various fields?

Assuming that it is something sane, like a tab character does it always output a tab if the field is missing?

For example if you have a record with just a publisher will the record be <tab><tab>Publisher

If it just has a price will it be <tab>price<tab>

Chaz
 
I'm having a hard time understanding your question.

You said &quot;But if a variable is blank, I don't want to print it.&quot;, but then said &quot;variables that are empty in the first file are not printed to my new file. That's where I am lost.&quot;

Is the sample output you included the desired output, or the actual output? If it's not what you're after, what is?

Do you want default values inserted? For example:

Hero=Spiderman,Price=4.50,Publisher=Marvel
Hero=Superman,Price=3.00
Hero=N/A,Price=2.00,Publisher=DC
Hero=Batman,Price=0.00,Publisher=DC

Or do you want to eliminate incomplete records? Is your problem how to parse the input file with fixed-width fields?

Maybe you could rephrase the question...
 
sorry for the confusion..I know how to get the variables..Even blanks are in the original file.

So I set the variables to the values in the input file

Input:

Spiderman 4.50 Marvel
Superman 3.00
2.00 DC
Batman DC

So my variables values for the above example would be:
Hero=Spiderman,Price=4.50,Publisher=Marvel
Hero=Superman,Price=3.00,Publisher=
Hero=,Price=2.00,Publisher=DC
Hero=Batman,Price=,Publisher=DC

Now the final output needs to be what is below:

Hero=Spiderman,Price=4.50,Publisher=Marvel
Hero=Superman,Price=3.00
Price=2.00,Publisher=DC
Hero=Batman,Publisher=DC


So anytime the variable is nothing, then I don't want to include it in my output.. I was thinking of maybe using the replace function, but was hoping to have to avoid any chance of error. Any suggestions?




 

Just before printing the variable you could....


If Instr(MyVariable,&quot;=&quot;) < Len(MyVariable) then
'Print it here because their is a value after = sign
End if
 
Can't tell how your input is formatted by the sample;
but you have obviously accomodated the input process.

Check to see what you empty input looks like. It is usually a empty string (0 = LEN(TRIM(input))). The debugger works great to get this.

Your print process needs to single step. Use the &quot;;&quot; to hold your place on the same line.

Print each output piece... if you data does not equal your known empty value, IF 0 <> LEN(TRIM(inputVal)) PRINT &quot;stuff&quot;;

Finally remember to finish your line, do not assume any one of the elements is last. For this you need a lone PRINT statement.

Oh yea, the comma...

Try DIM sComma = &quot;&quot;.

At the end of process loop: sComma = &quot;,&quot;. I know it gets redundant, but processors are up to 2Ghz now. Besides an assignment versus a conditional and branch is faster anyway.


Wil Mead
wmead@optonline.net

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top