Reverse text

S

Steven Burn

Just wondering if anyone is aware of any way to reverse the following so it
displays the text from

end of file > start of file

instead of

start of file > end of file


<%
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim ThisLine
Dim PrintLine
Set FileObject = Server.CreateObject("Scripting.FileSystemObject")
SomeFile = Server.MapPath("somefile.txt")
Set InputStream = FileObject.OpenTextFile(SomeFile, ForReading, False)
Do While Not InputStream.AtEndOfStream
ThisLine = InputStream.ReadLine
PrintLine = PrintLine + ThisLine + "<br>"
Loop
InputStream.Close
Set OutputStream = Nothing
Set FileObject = Nothing
Response.Write PrintLine
%>

--
Regards

Steven Burn
Ur I.T. Mate Group
www.it-mate.co.uk

Keeping it FREE!

Disclaimer:
I know I'm probably wrong, I just like taking part ;o)
 
G

Guest

Do you mean that the last line goes first still preserving the prder od
letters in it, or you want the last letter of the file to come first and so
on?
 
S

Steven Burn

Basically, the file contains comments such as below;

Date
Name
E-mail
Website
Comments

(see example at:
http://www.it-mate.co.uk/support/guestgen/viewguestbook.asp)

At present, the oldest is shown first (i.e. the first entry in the file) as
newer one's are simply appended to the file, so are thus, shown last (the
oldest entry being at the top of the page, and the newest, at the bottom).

I simply want to reverse the order in which the entries are shown, so the
newest is at the top, and the oldest, at the bottom. (if that makes sense?).

--
Regards

Steven Burn
Ur I.T. Mate Group
www.it-mate.co.uk

Keeping it FREE!

Disclaimer:
I know I'm probably wrong, I just like taking part ;o)
 
T

TomB

PrintLine should contain the entire file, so you should be able to .....

Dim iLoop
Dim ReversedPrintLine
for iLoop = len(PrintLine) to 1 step-1
ReversedPrintLine=ReversedPrintLine & Mid(PrintLine,iLoop,1)
next

I'm assuming somefile.txt is text.
 
T

TomB

I didn't think of that. You could do that (reverse line order but not
letters) by changing
PrintLine=PrintLine + ThisLine+"<BR>"
to
PrintLine=ThisLine+PrintLine+"<BR>"
 
S

Steven Burn

It's plain text yes but, unfortunately I've tried that and it reverses the
order of the entries aswell (see my reply to aa's reply).

--
Regards

Steven Burn
Ur I.T. Mate Group
www.it-mate.co.uk

Keeping it FREE!

Disclaimer:
I know I'm probably wrong, I just like taking part ;o)
 
C

Curt_C [MVP]

use two strings....
one to hold the person, one to hold the group.
Read in the Person info assigning it like
stringPer = stringPer + moreinfo

When each person is done assign it to the group string like this
stringGrp = stringPer + stringGrp
stringPer = "" (make sure to reset the stringPer each time)
 
S

Steven Burn

Cheers Curt, I'll give it a shot ;o)

--
Regards

Steven Burn
Ur I.T. Mate Group
www.it-mate.co.uk

Keeping it FREE!

Disclaimer:
I know I'm probably wrong, I just like taking part ;o)
 
D

Dave Anderson

Steven Burn said:
Just wondering if anyone is aware of any way to reverse
the following so it displays the text from

end of file > start of file

instead of

start of file > end of file

I assume you mean one line at a time. If the file isn't too large, string
concatenation shouldn't be too taxing on VBScript. Replace these two
lines...

ThisLine = InputStream.ReadLine
PrintLine = PrintLine + ThisLine + "<br>"

....with this:
PrintLine = InputStream.ReadLine() + "<br>" + PrintLine


In general, reversal of order is a great job for a stack. JScript lets you
treat any array like one. Then again, it has a reverse() method. For
completeness, here is a JScript alternative to string concatenation:

var a = new Array(), forReading = 1,
fso = Server.CreateOject("Scripting.FileSystemObject"),
file = fso_OpenTextFile("somefile.txt", forReading)
while (!file.AtEndOfStream) a.push(file.ReadLine())
file.Close()

At this point, you can either use Array.reverse()...
Response.Write(a.reverse().join("<BR>"))

....or a sequence of Array.pop() operations:
while (a.length) Response.Write(a.pop() + "<BR>")



--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 
G

Guest

Yes, It makes sense.
The cleanest way seems to use a database rather then a text file - you will
get much greater freedom of presentation the data.
Another option is to use XML rather then flat text
Alternatively you can read the lines into an array and then output the array
starting with the highest index
 
S

Steven Burn

Cheers for the reply. I'm trying to keep away from the database if I can as
the file's I've written are to be used by those with no experience in HTML
or ASP, and are to be placed on servers that don't have Access support (oh
the joys).

I'd like to look into the XML side of it and shall do so as soon as my time
allows, thankyou for the suggestion.

As for the array, I looked at that when I first wrote it and couldn't get
the stupid thing to work, so just left it alone. ;o)

--
Regards

Steven Burn
Ur I.T. Mate Group
www.it-mate.co.uk

Keeping it FREE!

Disclaimer:
I know I'm probably wrong, I just like taking part ;o)
 
S

Steven Burn

Cheers for the reply Dave. Unfortunately, neither of the solutions will work
as the entries in the text file (basically a flat text database) are along
the line's of;

date
name
email
website
comments

Thus, using the solutions provided would also reverse the way the above
appears. Instead (and I should have probably said in the first place), I'm
trying to get each actual "entry" (as above) reversed (if that makes
sense?).

I should probably just place a marker or something with each entry, but
decided not to (just to keep it explicitly simple, or so I thought at the
time ;o)).

--
Regards

Steven Burn
Ur I.T. Mate Group
www.it-mate.co.uk

Keeping it FREE!

Disclaimer:
I know I'm probably wrong, I just like taking part ;o)
 
D

Dave Anderson

Steven Burn said:
Thus, using the solutions provided would also reverse the way
the above appears. Instead (and I should have probably said in
the first place), I'm trying to get each actual "entry" (as
above) reversed (if that makes sense?).

I think it makes sense -- you have a repeating sequence of 5 lines, the
sequence of which you want to invert. Correct?



--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 
S

Steven Burn

Basically, yes.......

--
Regards

Steven Burn
Ur I.T. Mate Group
www.it-mate.co.uk

Keeping it FREE!

Disclaimer:
I know I'm probably wrong, I just like taking part ;o)
 
D

Dave Anderson

Steven Burn said:
Basically, yes.......

In that case, how about something like this?

Const ForReading = 1
Dim FileObject, SomeFile, InputStream, PrintLine, LineCount
Set FileObject = Server.CreateObject("Scripting.FileSystemObject")
SomeFile = Server.MapPath("somefile.txt")
Set InputStream = FileObject.OpenTextFile(SomeFile, ForReading, False)
LineCount = 0
Do While Not InputStream.AtEndOfStream
LineCount = LineCount + 1
PrintLine = InputStream.ReadLine() + "<br>" + PrintLine
If LineCount Mod 5 = 0 Then
Response.Write(PrintLine)
Else
PrintLine = ""
End If
Loop
InputStream.Close
Set OutputStream = Nothing
Set FileObject = Nothing


--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 
T

TomB

Is it possible to change the content of the text file when you are writing,
rather than reading? I'm sure you are doing an append, but if you could
read the contents of the text file in then replace the contents with the New
followed by the current then you'd have the text file in the order you want.

I assume you'd have more reads than writes, so this would (I'm guessing) be
a little more efficient.
 
G

Guest

eXcellent idea. I wish I though about this myself.
I suggest Steven to drop all other options and do just this
 
S

Steven Burn

Tom, short of using a DB, it certainly looks like it so I'll just do
that.....

Thanks for the replies everyone.

--
Regards

Steven Burn
Ur I.T. Mate Group
www.it-mate.co.uk

Keeping it FREE!

Disclaimer:
I know I'm probably wrong, I just like taking part ;o)
 
B

Brynn

You could go grab my TextToArray.asp script and do


<!-- #include virtual =
"/coolpier_scripts/_text_tools/TextToArray.asp" -->
<%
Dim yourArray
yourArray =
cp_TextFileToArray(Server.MapPath("/directory/yourTextFile"), "none")

n = ubound(yourArray)
Do Until n < 0


n = n - 1
Loop
%>

Brynn
www.coolpier.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top