Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 tokens
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
pokerface




PostPosted: Tue Nov 09, 2004 7:49 pm   Post subject: tokens

i would like to know how to out put a certain amount of tokens coming from a file

example:

if my text file has 10 words in it, i want to out put 2 words at a time right after each other
*********************file*************************
one two three four five six seven eight nine ten
********************end of file*********************
*************************************************
*******************output************************
one two
three four
five six
seven eight
nine ten
*******************end of output********************

do u know what i mean? im sorry that i always post new topics but i didnt find anything on tokens from files?
ty!
Sponsor
Sponsor
Sponsor
sponsor
myob




PostPosted: Tue Nov 09, 2004 8:18 pm   Post subject: (No subject)

okay i did a program that does that, hopefully it helps

this is the code for the program
code:

var file, counter : int
open : file, "data.txt", get
var data : flexible array 1 .. 0 of string
counter := 0
loop
    counter := counter + 1
    new data, counter
    get : file, skip
    get : file, data (counter)
    exit when eof (file)
end loop
for i : 3 .. counter + 2
    if i mod 2 = 1 then
        put data (i - 2), " " ..
    else
        put data (i - 2)
    end if
end for


this is what is in the file "data.txt"
code:

one two three four five six seven eight nine ten
pokerface




PostPosted: Tue Nov 09, 2004 10:10 pm   Post subject: (No subject)

yes your right about that but i want the user to say how many tokens they want to be on each line! my example was only a example! is there some other code that may do that?
myob




PostPosted: Wed Nov 10, 2004 2:26 am   Post subject: (No subject)

i see you just not very efficient with ur codes yet. look at the following codes and tell me if they do what u want, and check out how much of a different it brings comparing to my first set of codes. sometimes generalization is all you need.
code:
var file, counter : int
open : file, "data.txt", get
var data : flexible array 1 .. 0 of string
counter := 0
loop
    counter := counter + 1
    new data, counter
    get : file, skip
    get : file, data (counter)
    exit when eof (file)
end loop
var n : int
put "How many words do you want on a line?"
get n
for i : 1 + n .. counter + n
    if i mod n = 0 then
        put data (i - n)
    else
        put data (i - n), " " ..
    end if
end for
pokerface




PostPosted: Wed Nov 10, 2004 7:12 pm   Post subject: (No subject)

i have no idea what
Quote:
var data : flexible array 1 .. 0 of string
is? could u explain it to me or show me another way?
wtd




PostPosted: Wed Nov 10, 2004 9:00 pm   Post subject: (No subject)

pokerface wrote:
i have no idea what
Quote:
var data : flexible array 1 .. 0 of string
is? could u explain it to me or show me another way?


This is a declaration of an array of strings which can grow later in the program, if necessary. Declaring it with initial bounds of 1 .. 0 means that it has space for nothing, initially.
pokerface




PostPosted: Sat Nov 13, 2004 4:15 pm   Post subject: (No subject)

i think this is easier!
code:
var word : string
var newtext : int
var count : int := 0
var newword : string := ""
var list : int
open : newtext, "Newtext.t", get
assert newtext > 0
loop
    get : newtext, skip
    exit when eof (newtext)
    loop
        get : newtext, skip
        exit when eof (newtext)
        count := count + 1
        get : newtext, word
        if count not= 4 then
            put word + " " ..
        elsif count = 4 then
            put word ..
            put " "
            count := 0
            exit
        end if
    end loop
end loop

this is all i know how to do it? is there a way to get rud of the second loop without changing to much to my snytax?
pokerface




PostPosted: Mon Nov 15, 2004 10:50 pm   Post subject: (No subject)

Does anyone know how to right justify the output? Every that i tried always makes it funky!!! This is what i got so far!

code:
var text : int
open : text, "newText.txt", get
var maxLength : int := 10
var word : string
var lineLength : int := 0
var deleting : int
loop
    get : text, skip
    exit when eof (text)
    get : text, word
    if lineLength + length (word) < maxLength then
        put word, " " ..
        lineLength := lineLength + length (word) + 1
    else
        put ""
        put word, " " ..
        lineLength := length (word)
    end if
end loop
Sponsor
Sponsor
Sponsor
sponsor
HyperFlexed




PostPosted: Tue Nov 16, 2004 8:57 pm   Post subject: (No subject)

pokerface wrote:
Does anyone know how to right justify the output? Every that i tried always makes it funky!!! This is what i got so far!

code:
var text : int
open : text, "newText.txt", get
var maxLength : int := 10
var word : string
var lineLength : int := 0
var deleting : int
loop
    get : text, skip
    exit when eof (text)
    get : text, word
    if lineLength + length (word) < maxLength then
        put word, " " ..
        lineLength := lineLength + length (word) + 1
    else
        put ""
        put word, " " ..
        lineLength := length (word)
    end if
end loop


code:

proc RightAlign (alignThis:string)
Text.Locate (Text.WhatRow,Text.maxcol-length(alignThis))
put alignThis
end RightAlign
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: