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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looking for a string with variable values

Status
Not open for further replies.

lucensys

Programmer
Mar 9, 2005
7
US
I'm waiting for the string "blablabla,hh-mm,MM-dd-yy,blablabla",where "hh-mm" and "MM-dd-yy" are time and date and are variables and also a don't matter date and time only the substrings at the beigining and the end of the all string.
How can I write a code that ignore the values of date an time in this string and only looking for matching the substrings at the start and the end of the string?

Thanks in advance
 
In your example the substrings are separated with commas. If you can count on that, I would recommend using "split" and "lindex":
#starting with your string value in a string variable, st1
set tlist [split $st1 ,]
set value1 [lindex $tlist 0]
set value2 [lindex $tlist end]
#now do your thing on $value1 and $value 2


If the string is not reliably comma delimited, then what else do you know? Are the substrings against which you will test fixed? That is, are you testing the substring at the beginning against a fixed value test string, and likewise with the substring at the end? If so, then you might want to consider "string range". Let's say your string is, again st1, and your test strings are tst1 and tst2:
set ix [string length $tst1];#length of first test string
incr ix -1;#since string index starts at 0
set value1 [string range $st1 0 $ix]
set ix [string length $tst2];#length of second test string
incr ix -1;#same reason
set value2 [string range $st1 end-$ix end]


You can, of course, use regular expressions but its complicated and slow:
regexp "^($tst1).*($tst2)$" $st1 st2

Bob Rashkin
rrashkin@csc.com
 
Regexp with the idices option and an incrmenting string range reassignement ameliorates bongs complaints on the regexp stuff. Personally I'd come up with a regsub line that does
the trick.
Post a sample of the data for a regsub solution.
 
Thanks a lot guys for your help, but this is my problem more specifically so I’ll appreciate if you can provide me with a more specific advice since I just begin with Tcl some months ago.

This is the procedure that I’m using currently but it only works with fixed text not with variable text mixed in it.

proc Ifreceived {expectString} {
global textView beginIndex

#get the last index
set lastIndex [$textView index end]
if {[$textView search $expectString $beginIndex $lastIndex] != ""} {
set returnCondition 0
} else {
set returnCondition 1
}

return $returnCondition
}

This procedures tells me if the string that I looking for is present in the current text then what I need to know is how to skip the time and date string (that is variable) and continue searching the substring at the end of my expected string.
Some examples of the expected string are :

"comdac-1-2,EQPT:,CPINIT,SC,03-10,08-28-15,,,\"pack initialization in progress\""
"comdac-1-1,EQPT:,CPINIT,SC,03-10,08-29-37,,,\"pack initialization in progress\""
"comdac-1-2:CPINIT,CL,03-10,08-32-56:\"pack initialization finished""
"comdac-1-1:CPINIT,CL,03-10,08-33-25:\"pack initialization finished""
"comdac-1-1:DBMEMTR,SC,03-10,08-31-45:\"data memory update in progress\""
"comdac-1-1:DBMEMTR,CL,03-10,08-32-53:\"data memory update completed\""

A lot of different strings like this are arriving at my text widget constantly, but I need to know if just some string has arrived. So what I need to know is somehow modify my current procedure to ignore the date and time substrings and just looking for the substrings at the begining and the end of the complete string.

Basically I need to know when comdac-1-2 has finished with initialization but if I’m expecting the string "pack initialization finished" comdac-1-1 is also sending the same string when it finished then I need to look at the beginig of the string to see if the one that has finished is comdac-1-1 or comdac-1-2.

I guess that regexp colud be the best way to implement this but unfortunately I’ve not experience enough.

Again, thanks for your help.


 
Well, I think regexp is what you need but I need to know more about what the form of the "the substrings at the begining and the end of the complete string" might be. From your examples, it looks like the "substring and the end" might always be enclosed in "\"s. Is that for sure? Also, what is the "substring at the beginning"? comdac-1-1? comdac-1-1:DBMEMTR? comdac-1-1:DBMEMTR,CL?

Anyway, the "<text pathname> search" function takes a -regexp option so it would be easy to implement if you just give me (us) a clearer idea of what constitutes the substrings you want to key on.

Bob Rashkin
rrashkin@csc.com
 
Well, basically the substring at the begining could be the first part of the all string until where time string starts, something like "comdac-1-2,EQPT:,CPINIT,SC" (even "comdac-1-2" would be enough)and the substring a at the end could be "\"pack initialization in progress\"" and you're right this message is always enclosed by "\".That's all I need ,just to know that comdac-1-2 finished initialization and not comdac-1-1.
 
OK. There are others on this forum who are more adept than I at regular expressions; hopefully they will correct any mis-statements I make (marsd). That said, I think you want to match "comdac-" at the start of the string, followed by a "number-number", followed by a bunch of anything, followed by a "\<anything\" at the end of the string. In that case, I think the regular expression looks like:

{^(comdac-[0-9]-[0-9]).*(\\.*\\)$}

note that where I had used "" earlier, I now must use {} to allow the "[0-9]" brackets to be interpreted correctly.

In your earlier example, you used the text widget search command to find if the string occurred. That doesn't give you the option of matching the various parts; only finding a string that matches the pattern. It looks to me like you have a bunch of lines in your text widget and you can treat each line as your input string. So, without regard to performance, I think I would propose the following:

#[red]get the whole text area into a list of lines[/red]
set linelist [split [$textView get 0.0 end] \n]
#[red]loop over each line and look for your magic words[/red]
foreach line $linelist {
regexp {^(comdac-[0-9]-[0-9]).*(\\.*\\)$} $line mvar cvar endvar
#[red]now "cvar" is your "comdac-n-n" and "endvar" is your "\xxxx...xxx\"[/red]
switch $cvar {
comdac-1-1 {<whatever>}
comdac-1-2 {<whatever>}
default {continue}
}
}

or whatever logic you need to do inside the loop

Bob Rashkin
rrashkin@csc.com
 
Bongs approach is good but you can just break it down to
begin with.
If all you need is the confirmation string for comdac-1-2,
then something like this works:
Code:
(mars) 62 % set line {"comdac-1-2:CPINIT,CL,03-10,08-32-56:\"pack initialization finished""}
"comdac-1-2:CPINIT,CL,03-10,08-32-56:\"pack initialization finished""
(mars) 63 % regexp $pat $line
1
(mars) 64 % set pat {.*(comdac.*1-2).*(pack.*itializ.*finished).*}
.*(comdac.*1-2).*(pack.*itializ.*finished).*
(mars) 65 % regexp $pat $line all one two 
1
(mars) 66 % set all
"comdac-1-2:CPINIT,CL,03-10,08-32-56:\"pack initialization finished""
(mars) 67 % set one
comdac-1-2
(mars) 68 % set two
pack initialization finished
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top