LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-11-2011, 01:29 PM   #1
Stuart07
LQ Newbie
 
Registered: May 2006
Location: Manchester, NH
Distribution: CentOS5.5
Posts: 13

Rep: Reputation: 0
tcl foreach loop not reading variables


This loop is supposed to read each line of a list, if it starts with a digit, set that digit to the port number, if it contains a given pattern, assign that pattern to to a second variable and only write to the log file if "FAIL" is in the variable.

What I'm stuck with, is that the last if statement determining whether or not to write to the log file, does not work if I place it in the foreach loop. An error "can't read "flipstatus": no such variable" is generated even though that variable should have been defined in the loop above. Are there any obvious mistakes in the code or is there a different way to format the loop

Code:
set result $expect_out(buffer)

set format [split $result "\n"]

        set item1 [lindex $format end]

        set host [string trimright $item1 "#"]

foreach line $format {

        if { [string index $host end] eq "w" } {

                if { [string is digit [string range $line 2 3]] == 1 } {

                        set local_port [string range $line 2 3]

                                if { [string index [lsearch -inline -regexp $line ".../.*/.*"] end] eq "0" } {

                                        set flipstatus "[lsearch -inline -regexp $line ".../.*/.*"] FAIL"

                                } else { set flipstatus [lsearch -inline -regexp $line ".../.*/.*"]
                                 }

                }

        } elseif { [string index $host end] eq "e" } {

                if { [string is digit [string range $line 2 3]] == 1 } {

                        set local_port [string range $line 2 3]

                                if { [string index [lsearch -inline -regexp $line ".../.*/.*"] end] eq "9" } {

                                        set flipstatus "[lsearch -inline -regexp $line ".../.*/.*"] FAIL"

                                } else { set flipstatus [lsearch -inline -regexp $line ".../.*/.*"]
                                }
                        }
                    }
if { [string index $flipstatus end] eq "L" } {

       puts $log "$host\n $local_port, $flipstatus\n"
        }


}
Thanks Ahead
 
Old 03-11-2011, 03:45 PM   #2
Stuart07
LQ Newbie
 
Registered: May 2006
Location: Manchester, NH
Distribution: CentOS5.5
Posts: 13

Original Poster
Rep: Reputation: 0
Had braces in wrong place.

Fixed

Code:
if { [string index $host end] eq "w" } {

                if { [string is digit [string range $line 2 3]] | [string is digit [string index $line 3]] == 1 } {

                        set local_port [string range $line 2 3]
                        }

                if { [string index [lsearch -inline -regexp $line ".../.*/.*"] end] eq "0" } {

                        set flipstatus "[lsearch -inline -regexp $line ".../.*/.*"]     FAIL"

                } else { set flipstatus [lsearch -inline -regexp $line ".../.*/.*"]

                }
 
  


Reply

Tags
expect, foreach, loop, tcl, variable


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stupid g++ compiler does not even support foreach loop, similarly for Visual C++ xuancong Programming 12 01-09-2011 11:58 PM
conditional foreach loop in perl casperdaghost Programming 2 06-01-2010 05:23 PM
Perl wonky IF outcome inside a @list foreach loop OldGaf Programming 3 09-18-2009 11:21 AM
Perl - foreach loop reading only the last $scalar in a @list OldGaf Programming 3 08-08-2009 10:57 AM
Perl: Where am I in a foreach loop? jrtayloriv Programming 3 01-30-2005 11:43 PM


All times are GMT -5. The time now is 04:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration