Liberty BASIC Community Forum
« convert to data file »

Welcome Guest. Please Login or Register.
Jan 22nd, 2018, 5:58pm


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: convert to data file  (Read 159 times)
debluzz
Full Member
ImageImageImage


member is offline

Avatar




PM


Posts: 135
xx convert to data file
« Thread started on: Nov 23rd, 2017, 07:24am »

is there a routine that will take #1 file and make it look like #2 file

#1
1 23.58 20
2 19.58 21
3 31.27 32
4 20.89 21
5 11.70 31
6 25.37 31
7 16.62 27
8 14.08 33


#2
1 data 23.58, 20
2 data 19.58, 21
3 data 31.27, 32
4 data 20.89, 21
5 data 11.70, 31
6 data 25.37, 31
7 data 16.62, 27
8 data 14.08, 33


UNLESS: there is a way to use #1 file for my sorting
User IP Logged

tenochtitlanuk
Board Moderator

member is offline

Avatar




Homepage PM

Gender: Male
Posts: 1179
xx Re: convert to data file
« Reply #1 on: Nov 23rd, 2017, 12:06pm »

I'd use something like the following code...
Code:
    nomainwin

    CRLF$ =chr$( 13) +chr$( 10)

    open "number1file.txt" for input  as #fileIn
    open "number2file.csv" for output as #fileOut

    while eof( #fileIn) =0
        line input #fileIn, ch$
        for i =1 to 3
            c$ =word$( ch$, i, " ")

            #fileOut c$; " ";
            if i =1 then #fileOut "data ";
            if i =2 then #fileOut ", ";
        next i
        #fileOut CRLF$;
    wend

    close #fileIn
    close #fileOut

    end
 
User IP Logged

tsh73
Board Moderator

member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1732
xx Re: convert to data file
« Reply #2 on: Nov 23rd, 2017, 12:58pm »

sometimes using special tool (here AWK )really cuts some corners ;)
Code:
awk "{print $1 \" data \" $2 \", \" $3}" a.txt 


but staying in BASIC,
you better read your file into array. Later you can sort it as you wish.

Code:
fname$="a.txt"

'counts lines in a file
nLines = 0
open fname$ for input as #1
    while not(eof(#1))
        nLines=nLines+1
       line input #1, aLine$
    wend
close #1
print "nLines =";nLines
print

'dim array
dim arr(nLines, 3)

'read data into array
open fname$ for input as #1
    for i = 1 to nLines
       line input #1, aLine$
       for j = 1 to 3   '3 space-separated fields
           arr(i, j)=  val(word$(aLine$, j))
       next
    next
close #1


'now you can do anything with array. Like, print
for i = 1 to nLines
    print arr(i, 1),  arr(i, 2), arr(i, 3)
next
print
'or format line with DATA (really nt needed after data already in array)
for i = 1 to nLines
    print arr(i, 1);" data "; arr(i, 2);" "; arr(i, 3)
next
 
« Last Edit: Nov 23rd, 2017, 12:59pm by tsh73 » User IP Logged

damned Dog in the Manger
Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls