Property Lists: Accessing Items In

I've constructed a property list but am having trouble accessing items in the list.

I've simplified my code for testing purposes:

Global globalVariables
insert "val1:001" after globalVariables
insert "val2:002" after globalVariables
insert "val3:003" after globalVariables

put globalVariables

In debug mode the result of the "put", which looks the way I'd expect it to, is:
(val1:001,val2:002,val3:003)

put the number of properties in globalVariables into myNum
log "Number of properties in globalVariables = " && myNum

In debug mode this returns zero.

put the item number of "val3" within globalVariables into myPos
log "Position of 'val3' within globalVariables = " && myPos

In debug mode this returns zero.

In my actual code I want to refer to these values like this:
globalVariables.val2
and in this case get back 002

Thoughts on what I'm doing wrong?

Thanks!

Comments

  • Hi Aaron,

    Your code creates a list of strings, not a property list. You can easily check if something is a property list in debug mode like this:
    put globalVariables is a property list #=> false in your case
    
    What you want to do is something like this:
    Global globalVariables 
    put "001" into globalVariables.val1
    put "002" into globalVariables.val2
    put "003" into globalVariables.val3
    
    This should get you what you need.

    Happy New Year!
  • The piece I left out is that I'm trying to populate my list by parsing a text file. And right now the text file looks similar to how I want the list to look:

    val1:001
    val2:002
    val3:003

    So I was hoping to use the insert function so that I can iterate through my text file and just stuff the values into the list.

    Can you recommend a best practice for doing something like that?

    Thanks!
  • AaronKuehn wrote:
    The piece I left out is that I'm trying to populate my list by parsing a text file. And right now the text file looks similar to how I want the list to look:

    val1:001
    val2:002
    val3:003

    So I was hoping to use the insert function so that I can iterate through my text file and just stuff the values into the list.

    Can you recommend a best practice for doing something like that?

    Thanks!

    Ah yes. All you need to do then is something like this:
    Repeat with each line of file "path/to/file.txt"
      insert value(it) after global globalVariables
    end repeat
    
    The value() function will convert this from a string to a property list.

    Hope this helps.

    Allen
  • I think I'm with you, but I'm still having a problem.

    Here's the text from my data file:

    ;Define variables needed for test.
    ;No space between label and value.
    ;Case does not matter.
    SUT:eggplant-sut
    SUTLogin:eggplant

    Here's my script:

    try

    --LOOK IN THE FOLDER UP ONE LEVEL FROM THIS TEST SUITE FOR THE EGGPLANT.DAT FILE.
    set the Folder to "./"

    --CREATE A GLOBAL PROPERTY LIST TO HOLD ALL THE VARIABLES BELOW.
    Global globalVariables

    --PARSE THROUGH EGGPLANT.DAT AND VARIABLE-IZE THE INFORMATION.
    repeat with each line of file "Eggplant.dat"

    put it

    if it does not contain ";" Then --SEMICOLON INDICATES A COMMENT IN THE DATA FILE
    insert value(it) after globalVariables
    else
    end if

    end repeat

    put globalVariables

    put the number of properties in globalVariables into myNum
    log "Number of properties in globalVariables = " && myNum

    catch theError
    put theError

    end try


    And I'm attaching a screenshot of the Debug window. It doesn't like the colon in my data file. But I've tried numerous things to get past that, but haven't found the magic bullet.

    Thanks!
  • SenseTalkDougSenseTalkDoug ForumAdmin admin
    The insert command will always result in a list (not a property list) and the value() function will only work with properly formatted SenseTalk expressions (your data file would need parentheses on each line for this to work).

    There are a number of ways to achieve what you want, but using the split by operator may be the easiest in this case. Try something like this:
    repeat with each line of file "Eggplant.dat"
    	if it doesn't begin with ";" then put it & return after data
    end repeat
    
    put data split by return and colon into globalVariables
    
    Hopefully that will get you going.
  • Thanks so much!!! That works and gives me exactly the type of result I was looking for.

    Thanks all for your help!!

    --Aaron
Sign In or Register to comment.