AutoHotkey Community

It is currently May 27th, 2012, 1:38 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: June 2nd, 2009, 9:19 pm 
Offline

Joined: May 21st, 2007, 3:44 pm
Posts: 176
Location: USA
I had a need, so I scratched it.

Code:
/*
  version: 2009-06-03v01

  author: ScottMattes
 
  purpose: to dynamically prompt the user for values to replace certain parts of a text string

  Examples:

 
 
  ; substitutions with no defaults
  raw_text := "<@now@> is the time for all good <@men@> to come to the aid of their <@country@>"

  msg_text := text_substitue( raw_text )
 
  msgbox, With substitution:`n`n%raw_text% `n`n%msg_text%

 
 
  ; substitutions with defaults
  raw_text := "<@now:Noon@> is the time for all good <@men:employees@> to come to the aid of their <@country:microwave@>!"

  msg_text := text_substitue( raw_text )
 
  msgbox, With substitution:`n`n%raw_text% `n`n%msg_text%

 
 
  ; no substitutions needed
  raw_text := "text with no substitution needed"

  msg_text := text_substitue( raw_text )
 
  msgbox, Without substitution:`n`n%raw_text% `n`n%msg_text%
*/



text_substitue( i_text )
{
  OutputDebug, text_substitue: -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
  OutputDebug, text_substitue: incoming i_text=%i_text%
  startpos = 1

  loop
  {
    OutputDebug, text_substitue: ---------------------
    FoundPos := RegExMatch(i_text, "UP)<@(.*)@>", match, startpos)
   
    ifequal, foundpos, 0
    {
      break ; there are no more to find, time to wrap things up and leave
    }
    else
    {
      varname  := substr( i_text, matchpos1, matchlen1 )
      OutputDebug, text_substitue: varname=%varname%
     
      subval   := varname
      OutputDebug, text_substitue: subval=%subval%
     
      startpos := matchpos1 + 1
      OutputDebug, text_substitue: matchpos1=%matchpos1%
     
      ; if there is a default specified for the varname, use it
      ifinstring, varname, :
      {
        stringsplit, var, varname, :
        OutputDebug, text_substitue: varname=%varname% var1=%var1% var2=%var2%
       
        varname := var1
        vardefault := var2
        OutputDebug, text_substitue: varname=%varname% vardefault=%vardefault%
      }

      inputbox, r_value, Variable replacement, %i_text%`n`nPlease enter a value for '%varname%':, , , , , , , ,  %vardefault%
     
      ifequal, errorlevel, 0
      {
        StringReplace, i_text, i_text, <@%subval%@>, %r_value%
        OutputDebug, text_substitue: r_value=%r_value%
      }
      else
      {
        OutputDebug, text_substitue: user clicked Cancel in InputBox
      }
    }
    OutputDebug, text_substitue: i_text=%i_text%
  }

  OutputDebug, text_substitue: ---------------------
   
  OutputDebug, text_substitue: outgoing i_text=%i_text%
 
  return %i_text%
}


Thoughts, comments, suggestions, all are welcome.

EDIT: 2009-06-03v01
corrected startpos problem
added OutputDebug calls

_________________
-------------
Scott Mattes
Image
My small, and slowly growing, collection of scripts.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 20 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group