Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Sorting

 [1/5] from: depotcity::telus::net at: 16-May-2001 23:29


Hello... Here's a exercise... Sort the following read/lines into N: from shortest length? line to longest... N:[] one two three "four five six" ; length? = 4 one two "three four" ; length? = 3 one two three four five "six seven" ; length? = 6 one two three four five six "seven eight" ; length? = 7 one "two three four five" ; length? = 2 Where the string counts as 1. So you end up with ... N: [ one "two three four five" one two "three four" one two three "four five six" one two three four five "six seven" one two three four five six "seven eight" ] TBrownell

 [2/5] from: gjones05:mail:orion at: 17-May-2001 8:35


From: "Terry Brownell"
> Here's a exercise... > > Sort the following read/lines into N: from shortest length? line to
longest...
> N:[] > one two three "four five six" ; length? = 4
<<quoted lines omitted: 11>>
> one two three four five six "seven eight" > ]
Well, I feel like I'm cheating, but ... input.txt contains the following text: one two three "four five six" one two "three four" one two three four five "six seven" one two three four five six "seven eight" one "two three four five" Then: n: sort read/lines %/c/path/to/input.txt The result: foreach o n [print o] What I learned from this exercise was that sort in this context used the subseries length. I have to admit to being a bit surprised, but maybe I should not have been. --Scott Jones

 [3/5] from: joel:neely:fedex at: 17-May-2001 10:33


Hi, Terry, This is SEVERELY quick and dirty, but here goes... Terry Brownell wrote:
> Hello... > Here's a exercise...
<<quoted lines omitted: 7>>
> one "two three four five" ; length? = 2 > Where the string counts as 1.
I just hacked a bit at the interactive console, rather than writing a tidy function. That last bit is "left as an exercise for the reader"... ;-) (I assumed that the strategy was more important than the tidy packaging.) I prefered to determine the "length" once, sort on that, and then throw away the scaffolding.
>> foo: {one two three "four five six"
{ one two "three four" { one two three four five "six seven" { one two three four five six "seven eight" { one "two three four five"} Your original data, all as one string
>> boo: parse/all foo "^/"
== [{one two three "four five six"} {one two "three four"} {one two three four five "six seven"} {one two three four five six "seve... The equivalent of READ/LINES (I was too lazy to save it as a file and read it back in.
>> goo: []
== []
>> foreach hoo boo [
[ append goo reduce [length? parse hoo " " hoo] [ ] == [4 {one two three "four five six"} 3 {one two "three four"} 6 {one two three four five "six seven"} 7 {one two three four five s... This builds a length/value structure, based on the fact that PARSE by default leaves quoted substrings intact.
>> sort/skip goo 2
== [2 {one "two three four five"} 3 {one two "three four"} 4 {one two three "four five six"} 6 {one two three four five "six seven"... That sorts the pairs by length.
>> boo: []
== []
>> foreach [len val] sort/skip goo 2 [append boo val]
== [{one "two three four five"} {one two "three four"} {one two three "four five six"} {one two three four five "six seven"} {one t... That strips off the lengths, now that we're through using them.
>> foreach hoo boo [print hoo]
one "two three four five" one two "three four" one two three "four five six" one two three four five "six seven" one two three four five six "seven eight" The final output... -jn-

 [4/5] from: depotcity:telus at: 17-May-2001 8:55


That <i>was<i> unexpected.. try this one... two three "four five six" five six "three four" one two three four five "six seven" two three four five six seven "eight nine ten" nine "ten eleven twelve" again, sorted based on parsed length? and then alpha (but not necessary) also, the length? of the str is irrelevant. so.. N: [ nine "ten eleven twelve" five six "three four" two three "four five six" one two three four five "six seven" two three four five six seven "eight nine ten" ] Terry Brownell

 [5/5] from: gjones05:mail:orion at: 17-May-2001 11:39


From: "Terry Brownell"
<snip> > try this one...
<<quoted lines omitted: 4>>
> nine "ten eleven twelve" > again, sorted based on parsed length? and then alpha (but not
necessary)
> also, the length? of the str is irrelevant. > so..
<<quoted lines omitted: 5>>
> two three four five six seven "eight nine ten" > ]
Well, this one certainly helped to point out why the other was only accidentally correct. Now you've gone and forced me to do it the right way. Grumble, grumble ;-) input.txt contains: two three "four five six" five six "three four" one two three four five "six seven" two three four five six seven "eight nine ten" nine "ten eleven twelve" Then the code...: sort-check: func [a b] [ either ((length? to-block a) = (length? to-block b)) [ a < b ][ ((length? to-block a) < (length? to-block b)) ] ] n: sort/compare read/lines %/path/to/input.txt :sort-check foreach i n [print i] Yields: nine "ten eleven twelve" five six "three four" two three "four five six" one two three four five "six seven" two three four five six seven "eight nine ten" Next example, please?! ;-) (Actually I had been trying to figure out more complicated sort/compare's before, now I think I've got it!) --Scott Jones

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted