Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

expect,  using associative arrays

Posted on 2006-06-29
4
Medium Priority
?
2,369 Views
Last Modified: 2012-08-13
Can somebody give me example of using associative arrays within expect script?
I need to create a map from string to string, then access/get value by key.
0
Comment
Question by:ravenpl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 43

Author Comment

by:ravenpl
ID: 17007769
Also, is there a way to use if/else statement?
0
 
LVL 35

Assisted Solution

by:Duncan Roe
Duncan Roe earned 500 total points
ID: 17008221
All expect arrays are associative. For instance, you might put something like

if {[info exists xarray(fred)]} {<whatever ....>}

This tests if you allocated an array element suffix of fred to the array xarray.

You certainly can use if/else although "else" itself is in fact a "noise" word. You must have Tcl installed for expect to work, so try "man n if" for the full story:

20:22:46$ man n if
if(n)                 Tcl Built-In Commands                 if(n)



_________________________________________________________________________________________________________________________________________________________________________________________________________

NAME
       if - Execute scripts conditionally

SYNOPSIS
       if expr1 ?then? body1 elseif expr2 ?then? body2 elseif ... ?else? ?bodyN?
_________________________________________________________________________________________________________________________________________________________________________________________________________


DESCRIPTION
       The if command evaluates expr1 as an expression (in the same way that expr evaluates its argument).  The value of the expression must be a boolean (a numeric value, where 0 is false and anything
       is true, or a string value such as true or yes for true and false or no for false); if it is true then body1 is executed by passing it to the Tcl interpreter.  Otherwise expr2 is evaluated as an
       expression and if it is true then body2 is executed, and so on.  If none of the expressions evaluates to true then bodyN is executed.  The then and else arguments are optional ``noise words'' to
       make the command easier to read.  There may be any number of elseif clauses, including zero.  BodyN may also be omitted as long as else is omitted too.  The return value from the command is  the
       result of the body script that was executed, or an empty string if none of the expressions was non-zero and there was no bodyN.


KEYWORDS
       boolean, conditional, else, false, if, true



Tcl                                                         if(n)

======================================
Just read the 2nd sentence of your question :-/

Very basically:

set array(string1) <xlate string1>
(same for 2, 3, 4 &c.). Except you'd probably use read or gets to read your table into the array. On a query, you'd use the info exists construct outlined earlier to verify the string is known, and return the translated string if so.
BTW you could use tclsh instead of expect, unless you actually use expect's additional facilities as well.
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 500 total points
ID: 17013087
array set hash [list a 2 b 3 c 4]
array names hash
puts $hash(b)

# expect is tcl
0
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 500 total points
ID: 17013138
if {$hash(b)==3} {puts "yes"} else { puts "no" }
0

Featured Post

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question