...

View Full Version : Output like a list



luigicannavaro
09-05-2007, 03:53 PM
Hi,

How I do an output of a (table with large strings) into a list of single words?
(no full points, brackets, commas, etc.)

Example

from:

I wouldn't be president if I kept drinking. You get sloppy, can't make decisions, it clouds your reason, absolutely. I still remember the feeling of a hangover, even though I haven't had a drink in twenty years." He said he ate chocolate in the evenings after he swore off booze, because his body missed the sugar.

TO:




a
a
absolutely
after
ate
be
because
body
booze
can't
chocolate
clouds
decisions
drink
drinking
even
evenings
feeling
get
had
hangover
haven't
He
he
he
his
I
I
I
I
if
in
in
it
kept
make
missed
of
off
president
reason
remember
said
sloppy
still
sugar
swore
the
the
the
though
twenty
wouldn't
years
You
your
Thank you

Luigi

rafiki
09-05-2007, 04:38 PM
you could use the Split() function (similar to explode with php) and split by the space then for each the array and print out the results.

Daemonspyre
09-05-2007, 04:43 PM
I can tell you how to get a simple list, but array ordering (moving the items into the order you want above) is going to take a lot of work.

To get a simple list, do this:


<%
Function killChars(strWords)
dim badChars
dim newChars
badChars = array("!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", ",", "+", "-", "=", "~", "`", "[", "]", "{", "}", """", ";", ":", "<", ">", "?", ",", ".", "/", "|", "\")
newChars = strWords
for i = 0 to uBound(badChars)
newChars = replace(newChars, badChars(i), "")
next
killChars = newChars
end function

strText = objRS("your_database_record")
'
strWords = killChars(strText)
'
arrText = split(strWords," ")
'
for x = 0 to ubound(arrText)
response.write(arrText(x) & "<br />" & vbCrLf)
next
%>

Spudhead
09-05-2007, 05:07 PM
This might be how I'd do it:



<script language="JScript" runat="server">
function SortVBArray(arrVBArray) {
return arrVBArray.toArray().sort().join('\b');
}
</script>

<%
Function SortArray(arrInput)
SortArray = Split(SortVBArray(arrInput), Chr(8))
End Function

dim sSentence, oRE, sText, aWords
sSentence = "I wouldn't be president if I kept drinking. You get sloppy, can't make decisions, it clouds your reason, absolutely. I still remember the feeling of a hangover, even though I haven't had a drink in twenty years. He said he ate chocolate in the evenings after he swore off booze, because his body missed the sugar."
set oRE = new RegExp
oRE.Pattern = "[^a-zA-Z0-9\'\s]"
oRE.Global = True
sText = oRE.Replace(sSentence , "")
set oRE = nothing
aWords = split(sText, " ")
if isArray(aWords) then
aSortedWords = SortArray(aWords)
for i=lBound(aSortedWords) to uBound(aSortedWords)
response.write(aSortedWords(i) & "<br/>" & vbCrLf)
next
end if
%>



I've just c&p'd an array-sorting method from 4guysfromrolla - it doesn't seem to be quite right but a quick google for "VBScript sort array" isn't going to leave you short of alternatives. Either way, approach stays the same: use a regexp to strip out anything except alphanumeric and whitespace (I wouldn't trust my regexps too much but it seems to do the job here), create an array by splitting on the whitespace... after that you can do pretty much what you like with it. Any use?

luigicannavaro
09-06-2007, 10:11 AM
SpudHead and DaemonSpyre,

1. Both scripts works fine.
2. The script of Spudhead does the sort of string very nice
3. However the Spudhead scriptīs donīt the sort of output data of a table. Just output like single words, but not in sort order.
4. Indeed I beleve in DaemonSpyre "(...) but array ordering (moving the items into the order you want above) is going to take a lot of work".
5. I remember in Dbase languages (Dbase, Clipper, FoxPro and Visual FP) there is a function called "total to()". Thus (like Jimmy Neutron) I think, think, think... and perhaps the solution would be "insert" the list of words in another table - first step. 2nd step - insert the list of sorted words (from 2nd table) in 3rd. table with its frecuencies together.

My reasoning is right?

Thanks for all.


Luigi

Daemonspyre
09-06-2007, 01:17 PM
Your reasoning is correct, but there are multiple other options.

Here's a version of an ASP sorter for you...

Try this:


<%
Function killChars(strWords)
dim badChars
dim newChars
badChars = array("!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", ",", "+", "-", "=", "~", "`", "[", "]", "{", "}", """", ";", ":", "<", ">", "?", ",", ".", "/", "|", "\")
newChars = strWords
for i = 0 to uBound(badChars)
newChars = replace(newChars, badChars(i), "")
next
killChars = newChars
end function

strText = "I wouldn't be president if I kept drinking. You get sloppy, can't make decisions, it clouds your reason, absolutely. I still remember the feeling of a hangover, even though I haven't had a drink in twenty years."" He said he ate chocolate in the evenings after he swore off booze, because his body missed the sugar."
'
strWords = killChars(strText)
'
arrText = split(strWords," ")
'
max = ubound(arrText)

For i = 0 to max
For j = i+1 to max
if LCASE(arrText(i)) > LCASE(arrText(j)) then 'Here, if you want DESC order, change the > to a <
TemporalVariable = arrText(i)
arrText(i) = arrText(j)
arrText(j) = TemporalVariable
end if
next
next

response.write("The sorted values are these ones: <br />" & vbCrLf)

for i=0 to max
response.write (arrText(i) & "<br />" & vbCrLf)
next
%>


Couple items of note:

1) If this is for your homework, George (CF Admin) is going to kill me... ;)

2) Notice the LCASE line above. The reason for this is that STRING arrays are sorted first by CASE, then by letter. If you take out the LCASE, you will get all uppercase words first, then all lowercase words.

3) There is a comment on how to change the sorting by ASC or DESC.

HTH!

PremiumBlend
09-06-2007, 03:25 PM
1) If this is for your homework, George (CF Admin) is going to kill me...

I remember this assignment from a few years ago. Except mine was supposed to load a text file in Java and display it.:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum