AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Newbie question: remove line breaks, replace with commas

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Wolfer



Joined: 08 Nov 2005
Posts: 14
Location: Chicago, IL

PostPosted: Thu Apr 10, 2008 7:23 am    Post subject: Newbie question: remove line breaks, replace with commas Reply with quote

I am a physician working with an electronic medical record system. I am looking for a script that will take a column of poorly formatted words (usually quickly typed by a secretary), gobble them up, and spit them back formatted with commas.

i.e. take this list:
HEADACHE
Sore Throat
fever
COugh

and turn it into this:
headache, sore throat, fever, cough

For bonus points, put an 'and' before the last symptom, i.e.
headache, sore throat, fever and cough.

AHK, even at the basic level I've mastered, has done some amazing things for me, and has saved me a lot of time, but I have already spent way too much time trying to figure this one out, and I actually have this day job...

Thanks.
Back to top
View user's profile Send private message
tonne



Joined: 06 Jun 2006
Posts: 1163
Location: Denmark

PostPosted: Thu Apr 10, 2008 8:16 am    Post subject: Reply with quote

Mark the text and press F11:
Code:
f11::
  Clipboard =
  Send ^c
  ClipWait 1
  StringLower, words, clipboard
  text =
  matchlast =
  loop, parse, words, `n
  {
    if A_LoopField
    {
      if matchlast
        text .= (text ? ", " : "") . matchlast
      RegExMatch(A_LoopField,"U)^\s*(?P<last>.*)\s*$",match)
    }
  }
  if matchlast
    text .= (text ? " and " : "") . matchlast
  clipboard := text
  send ^v ; overwrites the selected text
return 

_________________
there's a dog barking close within the range of my ear
sounds like he wants to escape the chain
he would probably bite me to death if he could
but the chain lets me spit in his face

- Kashmir
Back to top
View user's profile Send private message
garry



Joined: 19 Apr 2005
Posts: 1035
Location: switzerland

PostPosted: Thu Apr 10, 2008 8:51 am    Post subject: Reply with quote

example reads file
Code:
   transform,S,chr,32       ;space
   transform,ten,chr,10     ;LineFeed
   transform,tre,chr,13     ;Carriage Return
   CF=%TRE%%TEN%


F1=test1.txt
F2=test2.txt
filedelete,%F2%
; create test file
ifnotexist,%F1%
  {
  Fileappend,HEADACHE`r`n,%F1%
  Fileappend,Sore Throat`r`n,%F1%
  Fileappend,fever`r`n,%F1%
  Fileappend,COugh`r`n,%F1%
  }

I=0
Q=0
loop,read,%F1%
  {
  I++
  }

I1:=(I-1)
loop,read,%F1%
  {
  Q++
  LR=%A_LoopReadLine%
  Stringreplace,LR1,LR,%CF%,,all
  if Q=%I1%
    {
    Fileappend,%LR%`, and%S% ,%F2%
    continue
    }

  if Q=%I%
    {
    Fileappend,%LR%`r`n,%F2%
    break
    }

  Fileappend,%LR1%`,,%F2%
  }
run,%F2%
exitapp
Back to top
View user's profile Send private message
Wolfer



Joined: 08 Nov 2005
Posts: 14
Location: Chicago, IL

PostPosted: Thu Apr 10, 2008 11:20 pm    Post subject: Great! Reply with quote

Code from tonne works beautifully! Pure wizardry to this newbie.

Script from garry creates new file, but how do you work it with different lists of words or symptoms?

One last request: the lists come out of our system formatted with two spaces before each line of symptoms. Can the code be tweaked to remove those spaces?
Back to top
View user's profile Send private message
tonne



Joined: 06 Jun 2006
Posts: 1163
Location: Denmark

PostPosted: Fri Apr 11, 2008 8:37 am    Post subject: Reply with quote

Change the RegExMatch line to:
Code:
      RegExMatch(A_LoopField,"U)^\s*\b(?P<last>.*)\s*$",match)

Garry's version is file based converting one file with a the list into another file with one formatted line. Don't get confused by the script making its own demo data.
_________________
there's a dog barking close within the range of my ear
sounds like he wants to escape the chain
he would probably bite me to death if he could
but the chain lets me spit in his face

- Kashmir
Back to top
View user's profile Send private message
garry



Joined: 19 Apr 2005
Posts: 1035
Location: switzerland

PostPosted: Fri Apr 11, 2008 10:55 am    Post subject: Reply with quote

thank you tonne for the regex example
Wolfer, I created a new file only for test, remove this fileappend example in script
leading spaces are removed (see also the command autotrim,off/on )
Quote:

Stringreplace,LR1,LR,%CF%,,all ;removes ascii 13 and 10 ( see also `r`n)
Fileappend,%LR1%`,,%F2% ;adds line and comma (`r`n is missing so adds each readed line to the same line

you can add this command to select your desired existing file (before loop )
Code:
FileSelectFile,F1,, ,Select a file, Text Documents (*.txt; *.doc)
if F1=
    return
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group