• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: getting chunks of text
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: getting chunks of text


  • Subject: Re: getting chunks of text
  • From: kai <email@hidden>
  • Date: Fri, 25 Mar 2005 18:13:25 +0000


On Fri, 25 Mar 2005 14:51:55 +0100, Christian Vinaa wrote:

At 19:40 -0800 16/03/2005, Scott Haneda wrote:
Given a string of length unknown, I need to chop it up to less than 256
characters, however, I do not want to chop a word in half, so scan backwards
to the first space before 256 chars. Not to worried about what would happen
when running into punctuation and such.


I need a result set that I can repeat through, thanks.

a bit late in reply but how about this:

set theText to "Mae seryddwyr yn dweud eu bod wedi canfod beth maent yn
credu yw galaeth anweledig newydd. Mae'r tîm, sy'n cael eu harwain gan
Brifysgol Caerdydd, yn honni mai dyma'r cyntaf i'w chanfod sydd wedi ei
wneud o fater tywyll. Ardal o fewn y bydysawd yn cynnwys cyfanswm mawr o
fás sy'n troi fel galaeth ond nad yw'n cynnwys sêr yw galaeth dywyll.
Cafodd ei darganfod 50 miliwn o flynyddoedd goleuni i ffwrdd drwy
ddefnyddio telesgopau radio yn Sir Gaer a Puerto Rico."
set text1 to characters 1 through 256 of theText as text
set lastWordNumber to count of words in text1
set textWordA to word lastWordNumber of theText
set textWordB to word lastWordNumber of text1
if textWordA = textWordB then
set myFinalText to words 1 through lastWordNumber of theText
else
set myFinalText to words 1 through (lastWordNumber - 1) of theText
end if



( and I have no doubt that Kai can do a oneliner ! )


Hmm... I'm flattered by your confidence, Christian, but I seriously doubt that I could come up a vanilla one-liner - especially since I believe the OP was after a list of string chunks, each shorter than 256 characters (rather than a list of words, say).

Since such a problem involves an initial string of indeterminate length, a repeat loop (or a recursive handler) would probably be the best way forward - so that's at least 3 or 4 lines before we even start! :-)

If I remember rightly, there were some pretty good answers when this thread originally broke. For robustness and sheer speed, Nigel Garvey is usually the man to watch - and I seem to recall that he suggested a 2-pointer approach. This would almost certainly be the method of choice for maximum efficiency.

However, since you've thrown down the 'brevity' gauntlet, here's an impromptu vanilla idea off the top of my head - which shouldn't be too much of a slouch, speed-wise:

---------------------------

to getChunks from t under n
set l to {}
repeat
set c to count t
if c < n then return l & t
set l's end to text 1 thru word -2 of (t's text 1 thru (1 + n - n div c))
set t to t's text ((count result) + 1) thru -1
end repeat
end getChunks


set theText to "Mae seryddwyr yn dweud eu bod wedi canfod beth maent yn credu yw galaeth anweledig newydd. Mae'r tîm, sy'n cael eu harwain gan Brifysgol Caerdydd, yn honni mai dyma'r cyntaf i'w chanfod sydd wedi ei wneud o fater tywyll. Ardal o fewn y bydysawd yn cynnwys cyfanswm mawr o fás sy'n troi fel galaeth ond nad yw'n cynnwys sêr yw galaeth dywyll. Cafodd ei darganfod 50 miliwn o flynyddoedd goleuni i ffwrdd drwy ddefnyddio telesgopau radio yn Sir Gaer a Puerto Rico."

getChunks from theText under 256

---------------------------

I'm still not sure I can agree with the sentiments expressed in 'theText' above, though... ;-)

---
kai
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: Importing to FileMaker
  • Next by Date: Re: Pref. pane (need more help)
  • Previous by thread: Re: getting chunks of text
  • Next by thread: tell a script to tend to system process for X amount of time?
  • Index(es):
    • Date
    • Thread