Go Back   CodingForums.com > :: Server side development > ASP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-05-2007, 03:53 PM   PM User | #1
luigicannavaro
Regular Coder

 
luigicannavaro's Avatar
 
Join Date: Aug 2007
Posts: 150
Thanks: 11
Thanked 0 Times in 0 Posts
luigicannavaro is an unknown quantity at this point
Cool Output like a list

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:
Quote:
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:


Quote:
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
luigicannavaro is offline   Reply With Quote
Old 09-05-2007, 04:38 PM   PM User | #2
rafiki
Senior Coder

 
rafiki's Avatar
 
Join Date: Aug 2006
Location: Floating around somewhere...
Posts: 2,034
Thanks: 18
Thanked 42 Times in 42 Posts
rafiki will become famous soon enough
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.
__________________
Get Firefox Now
rafiki is offline   Reply With Quote
Old 09-05-2007, 04:43 PM   PM User | #3
Daemonspyre
Regular Coder

 
Join Date: Mar 2007
Posts: 505
Thanks: 1
Thanked 19 Times in 19 Posts
Daemonspyre is on a distinguished road
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:

Code:
<%
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
%>
__________________
Quote:
To say my fate is not tied to your fate is like saying, 'Your end of the boat is sinking.' -- Hugh Downs
Please, if you found my post helpful, pay it forward. Go and help someone else today.
Daemonspyre is offline   Reply With Quote
Users who have thanked Daemonspyre for this post:
luigicannavaro (09-06-2007)
Old 09-05-2007, 05:07 PM   PM User | #4
Spudhead
Senior Coder

 
Spudhead's Avatar
 
Join Date: Jun 2002
Location: London, UK
Posts: 1,856
Thanks: 8
Thanked 110 Times in 109 Posts
Spudhead is on a distinguished road
This might be how I'd do it:

Code:
<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?
Spudhead is offline   Reply With Quote
Users who have thanked Spudhead for this post:
luigicannavaro (09-06-2007)
Old 09-06-2007, 10:11 AM   PM User | #5
luigicannavaro
Regular Coder

 
luigicannavaro's Avatar
 
Join Date: Aug 2007
Posts: 150
Thanks: 11
Thanked 0 Times in 0 Posts
luigicannavaro is an unknown quantity at this point
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
luigicannavaro is offline   Reply With Quote
Old 09-06-2007, 01:17 PM   PM User | #6
Daemonspyre
Regular Coder

 
Join Date: Mar 2007
Posts: 505
Thanks: 1
Thanked 19 Times in 19 Posts
Daemonspyre is on a distinguished road
Your reasoning is correct, but there are multiple other options.

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

Try this:

Code:
<%
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!
__________________
Quote:
To say my fate is not tied to your fate is like saying, 'Your end of the boat is sinking.' -- Hugh Downs
Please, if you found my post helpful, pay it forward. Go and help someone else today.

Last edited by Daemonspyre; 09-06-2007 at 01:23 PM..
Daemonspyre is offline   Reply With Quote
Old 09-06-2007, 03:25 PM   PM User | #7
PremiumBlend
Regular Coder

 
PremiumBlend's Avatar
 
Join Date: Apr 2006
Location: Marion, Iowa
Posts: 201
Thanks: 0
Thanked 13 Times in 13 Posts
PremiumBlend is an unknown quantity at this point
Quote:
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.
__________________
My Website: DumpsterDoggy
PremiumBlend is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:37 PM.


Advertisement
Log in to turn off these ads.