Array Values for Dialog

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
edauthier
Pro Scripter
Posts: 84
Joined: Sun Apr 13, 2003 1:26 pm
Location: USA

Array Values for Dialog

Post by edauthier » Tue Aug 21, 2007 8:53 pm

I'm looking for the most efficient way to read an array and utilize for Dialog input validation.

In the below script I reference a text file that contains:
Sally
Henry
Jones
Harold
(c:\1\data.txt)

So when I run the dialog I want to make sure that MyDialog.MyEdit1 contains one of the values from the array. I receive equals for all input now.

Would you have to read each value individually and write it to an individual file for each name in the array? (which seems to defeat the purpose of an array)

Here's a simple example. Thanks in advance, Ed

Code: Select all

ReadFile>c:\1\data.txt,file

Dialog>MyDialog
   Caption=ArrayTest
   Width=445
   Height=250
   Top=CENTER
   Left=CENTER
   Max=1
   Min=1
   Close=1
   Resize=1
   Edit=MyEdit1,13,46,230,
   Button=Test Script,64,110,129,20,3
   Button=Exit Macro,277,58,100,20,2
   Label=Enter Value,16,32,true
EndDialog>MyDialog


Label>MainLoop
  Show>MyDialog,result
  If>result=2,End
  If>result=3,SayHello
Goto>MainLoop


srt>SayHello

If>{(%MyDialog.MyEdit1%<file>Message1
Else
GoSub>Message2
EndIf
End>SayHello


srt>Message1
MDL>In Array
end>Message
goto>End

srt>Message2
MDL>Not in Array
end>Message
goto>End


Label>End

edauthier
Pro Scripter
Posts: 84
Joined: Sun Apr 13, 2003 1:26 pm
Location: USA

Post by edauthier » Tue Aug 21, 2007 9:00 pm

Sorry, Had to disable HTML for the in Script.

Code: Select all

Dialog>MyDialog
   Caption=ArrayTest
   Width=445
   Height=250
   Top=CENTER
   Left=CENTER
   Max=1
   Min=1
   Close=1
   Resize=1
   Edit=MyEdit1,13,46,230,
   Button=Test Script,64,110,129,20,3
   Button=Exit Macro,277,58,100,20,2
   Label=Enter Value,16,32,true
EndDialog>MyDialog


Label>MainLoop
  Show>MyDialog,result
  If>result=2,End
  If>result=3,SayHello
Goto>MainLoop


srt>SayHello

If>{(%MyDialog.MyEdit1%<>%file%)}
GoSub>Message1
Else
GoSub>Message2
EndIf
End>SayHello


srt>Message1
MDL>In Array
end>Message
goto>End

srt>Message2
MDL>Not in Array
end>Message
goto>End


Label>End
Last edited by edauthier on Tue Aug 21, 2007 9:02 pm, edited 1 time in total.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Aug 21, 2007 9:01 pm

ReadFile>filename,data
Separate>data,CRLF,lines
Let>MyDialog.msEdit1=lines_3
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

edauthier
Pro Scripter
Posts: 84
Joined: Sun Apr 13, 2003 1:26 pm
Location: USA

Post by edauthier » Tue Aug 21, 2007 9:14 pm

Thanks Marcus,
Forgive me, but with that snippet would I then have to write an else for each read line?

i.e. -

If>{(%MyDialog.MyEdit1%%Line_1%)}
Else
If>{(%MyDialog.MyEdit1%%Line_2%)}
Else
If>{(%MyDialog.MyEdit1%%Line_3%)}

Gosub>Message1
Else
GoSub>Message2
EndIf
End>SayHello

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Aug 21, 2007 9:17 pm

Why not just:

ReadFile>filename,data
Position>MyDialog.msEdit1%CRLF%,data,1,p
If>p>0
MessageModal>contains one of the items
Endif
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue Aug 21, 2007 9:51 pm

Or.... you can use separate to "find" a string in your text data.

Plagiarizing most of Marcus' sample:

ReadFile>filename,data
Separate>%data%,%MyDialog.msEdit1%,var
If>%var_count%>1
MessageModal>%filename% contains %MyDialog.msEdit1%
Endif

edauthier
Pro Scripter
Posts: 84
Joined: Sun Apr 13, 2003 1:26 pm
Location: USA

Post by edauthier » Tue Aug 21, 2007 11:42 pm

Thanks Dick,
I am not clear how the complete script would be contructed with your last snippet. It seems so simple, yet I cannot work it out efficiently.
Thanks for the tip..

? The value is always false like this:

[code]
ReadFile>c:\1\data.txt,data

Dialog>MyDialog
Caption=ArrayTest
Width=445
Height=250
Top=CENTER
Left=CENTER
Max=1
Min=1
Close=1
Resize=1
Edit=MyEdit1,13,46,230,
Button=Test Script,64,110,129,20,3
Button=Exit Macro,277,58,100,20,2
Label=Enter Value,16,32,true
EndDialog>MyDialog


Label>MainLoop
Show>MyDialog,result
If>result=2,End
If>result=3,SayHello
Goto>MainLoop


srt>SayHello
Separate>%data%,%MyDialog.msEdit1%,var
If>%var_count%>1

MDL>Is in Array
Goto>End
Else
MDL>Not in Array
Goto>End
Endif
End>SayHello

Goto>End


Label>End
[/code]

edauthier
Pro Scripter
Posts: 84
Joined: Sun Apr 13, 2003 1:26 pm
Location: USA

Post by edauthier » Wed Aug 22, 2007 1:19 am

When working with Dialogs it seems like you 'must' define your read lines after reading a file and indexing the returns. (ReadLn_1,2,3, etc.)


This more traditional approach produces a positive return for all entries into MyDialog.MyEdit1

Code: Select all

Let>k=1
Label>start
ReadLn>c:\1\data.txt,k,line
//c:\1\data.txt contains Sally,Henry,Jones,Harold
If>line=##EOF##,finish
Separate>%line%,%CRLF%,array_values
Let>k=k+1
Goto>start
Label>finish

Dialog>MyDialog
   Caption=ArrayTest
   Width=445
   Height=250
   Top=CENTER
   Left=CENTER
   Max=1
   Min=1
   Close=1
   Resize=1
   Edit=MyEdit1,13,46,230,
   Button=Test Script,64,110,129,20,3
   Button=Exit Macro,277,58,100,20,2
   Label=Enter Value,16,32,true
EndDialog>MyDialog


Label>MainLoop
  Show>MyDialog,result
  If>result=2,End
  If>result=3,SayHello
Goto>MainLoop


srt>SayHello
If>(%MyDialog.MyEdit1%<>array_values)
MDL>Is in Array
        Goto>End
Else
MDL>Not in Array
        Goto>End
Endif
End>SayHello
 
Label>End

edauthier
Pro Scripter
Posts: 84
Joined: Sun Apr 13, 2003 1:26 pm
Location: USA

Post by edauthier » Wed Aug 22, 2007 3:26 am

Correction to last code. The Message was transposed for the branching. Based on If>(%MyDialog.MyEdit1%array_values) the 'true' return would follow thru to: Not in Array.

Code: Select all

srt>SayHello
If>(%MyDialog.MyEdit1%<>array_values)
MDL>Not in Array
        Goto>End
Else
MDL>In Array
        Goto>End
Endif
End>SayHello

User avatar
pgriffin
Automation Wizard
Posts: 460
Joined: Wed Apr 06, 2005 5:56 pm
Location: US and Europe

Post by pgriffin » Thu Aug 23, 2007 3:48 am

Could you tell us if it matters which of these values from the text file ends up in your edit field? Why not put ALL the values in a ComboBox so you could select the one you want.

I think what might be confusing everyone is that it seems random as to what you are trying to accomplish with your dialog.

edauthier
Pro Scripter
Posts: 84
Joined: Sun Apr 13, 2003 1:26 pm
Location: USA

Post by edauthier » Thu Aug 23, 2007 4:05 am

It's really about using an array behind the input box in the dialog and then referencing it as a single entity in MS script. Nothing more.

I am not sure I can give you an applied example. Here's a shot at it.

Let's just say is was part of a security question/answer routine and this text file contained the 'secret answer'. The text file would be changed daily.

I hope this is a fair enough example and eliminates combo box question.

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Thu Aug 23, 2007 4:10 am

Your fourth post in this thread works for me after fixing the typo.

ReadFile>c:\1\data.txt,data

Dialog>MyDialog
Caption=ArrayTest
Width=445
Height=250
Top=CENTER
Left=CENTER
Max=1
Min=1
Close=1
Resize=1
Edit=MyEdit1,13,46,230,
Button=Test Script,64,110,129,20,3
Button=Exit Macro,277,58,100,20,2
Label=Enter Value,16,32,true
EndDialog>MyDialog


Label>MainLoop
Show>MyDialog,result
If>result=2,End
If>result=3,SayHello
Goto>MainLoop


srt>SayHello
Separate>%data%,%MyDialog.myEdit1%,var
If>%var_count%>1

MDL>%MyDialog.myEdit1% Is in Array
Goto>End
Else
MDL>%MyDialog.myEdit1% Not in Array
Goto>End
Endif
End>SayHello

Goto>End


Label>End

edauthier
Pro Scripter
Posts: 84
Joined: Sun Apr 13, 2003 1:26 pm
Location: USA

Post by edauthier » Thu Aug 23, 2007 4:22 am

Wow... Thanks for assistance. That's what can happen when you stare at something too long. :shock:

Your snippet was the clencher. I owe ya one!!

Cheers.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts