Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TCL/EXPECT - Removing last line from array element 1

Status
Not open for further replies.

ianfo

Programmer
Aug 20, 2002
29
GB

This is related to the unix expect tool that uses TCL so I hope the question is still relevent here.

If I have an array element expect_out(buffer) that has a multiple line content like this....

AAAAAAA
BBBBBBB
CCCCCCC
ABCD$

How would I go about removing the last line?

I've tried using string trimright but can only get it to remove the last character...

Thanks for any help you can give me
 
There are several different approaches you could take. One would be to use string last to find the index of last occurrence of a newline (\n), and then string range to extract the substring from the beginning to just before that point:

[tt]% set output "AAAAAAA
> BBBBBBB
> CCCCCCC
> ABCD$"
AAAAAAA
BBBBBBB
CCCCCCC
ABCD$
% [ignore]set index [string last "\n" $output][/ignore]
23
% [ignore]incr index -1[/ignore]
22
% [ignore]set desired [string range $output 0 $index][/ignore]
AAAAAAA
BBBBBBB
CCCCCCC[/tt]

Another would be to use regular expressions, if you're comfortable with those. Taking advantage that regular expressions are "greedy" by default (they try to match as many characters as possible), you could get by with this:

[tt]% regexp {(.*)\n} $output match desired
1
% puts $desired
AAAAAAA
BBBBBBB
CCCCCCC[/tt]

As you're using Expect, you might be able to incorporate the subpattern directly into your expect pattern. Something like this:

Code:
expect {
  -re {(.*)\r\nABCD\$}  {
      # If this matches, expect_out(0,string)
      # contains all the characters matched,
      # and expect_out(1,string) contains the
      # characters matching the first
      # subpattern (the "(.*)" match).
   }
   timeout {
      # Do whatever timeout stuff you need if
      # the pattern doesn't match.
   }
   eof {
      # Handle getting eof notification from
      # the spawned process shutting down.
}

Note that when you're using Expect, if you're reading output from a spawned process, you've also got to take into account that the end-of-line sequence is almost certainly going to be a carriage return-newline sequence (\r\n) instead of just a newline (\n). That means that in the previous examples I showed, you'd have to account for that as well. For example:

[tt]set index [ignore][string last [/ignore]"\r\n"[ignore] $output]
incr index -1
set desired [string range $output 0 $index][/ignore][/tt]

or

[tt]regexp {(.*)\r\n} $output match desired[/tt] - Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top