#122580 - 2004-07-11 03:20 PM
Re: Strip leading spaces?
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Well you do not state what version of KiXtart you are using, but looking at the 4.22 manual yields:
Code:
LTrim( )
Action: Strips leading spaces from an input string and returns the result. Syntax: LTRIM ("string") Parameters: String The string from which to strip leading spaces. Returns: The input string without leading spaces. See Also: RTrim( ), Trim( ) Example: $x = LTRIM(SUBSTR(@IPADDRESS0, 1, 3))
If you want more control to match and extract the value you should look at using Regular Expressions. Checkout this link: RFC: Regular Expression UDFs
Let me know if you have any further questions.
|
|
Top
|
|
|
|
#122583 - 2004-07-11 08:52 PM
Re: Strip leading spaces?
|
brewdude6
Hey THIS is FUN
Registered: 2000-10-21
Posts: 280
Loc: Nashville, TN
|
Sorry for not posting more of the code. Les, I believe this is something that you've written and I've made a mess of. If not, I apologize to whoever did and not giving them proper credit. BTW Howard, I'm using 4.22.
Code:
$null = SetOption ("NoVarsInStrings","On") $null = SetOption ("Explicit","On") Dim $file,$filehandle,$namevalue,$line,$name,$namearray,$null,$outputfile,$outputhandle $file = 'c:\kix\test_text.txt' $filehandle = FreeFileHandle() If Open ($filehandle,$file) = 0 $namevalue = 0 While @Error = 0 $line = ReadLine ($filehandle) If InStr ($line,'DCS Application Snapshot') ReDim Preserve $namearray[$namevalue] $namearray[$namevalue] = $line $namevalue = $namevalue+1 EndIf If InStr ($line,'Authorization') ReDim Preserve $namearray[$namevalue] $namearray[$namevalue] = $line $namevalue = $namevalue+1 EndIf If InStr ($line,'Inbound communication address') ReDim Preserve $namearray[$namevalue] $namearray[$namevalue] = $line $namevalue = $namevalue+1 EndIf If InStr ($line,'Outbound communication address') ReDim Preserve $namearray[$namevalue] $namearray[$namevalue] = $line $namevalue = $namevalue+1 EndIf If InStr ($line,'Application name') ReDim Preserve $namearray[$namevalue] $namearray[$namevalue] = $line $namevalue = $namevalue+1 EndIf If InStr ($line,'Application status') ReDim Preserve $namearray[$namevalue] $namearray[$namevalue] = $line $namevalue = $namevalue+1 EndIf Loop $null = Close ($filehandle) $outputfile = 'c:\kix\test_text_out.txt' $outputhandle = FreeFileHandle() If Exist ($outputfile) Del $outputfile EndIf If $namearray[0] <> '' If Open ($outputhandle,$outputfile,5) = 0 For Each $name In $namearray $null = WriteLine ($outputhandle,$name+@CRLF) Next $null = Close ($outputhandle) Else $null = MessageBox ('Unable To Create '+$outputfile,'Error',0) EndIf EndIf Else $null = MessageBox ('Unable To Open '+$file,'Error',0) EndIf
_________________________
I could have made a neat retort but didn't, for I was flurried and didn't think of it till I was downstairs. -Mark Twain
|
|
Top
|
|
|
|
#122584 - 2004-07-11 09:15 PM
Re: Strip leading spaces?
|
Les
KiX Master
   
Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
|
I don't recognize that code as mine and if I did I would deny it. :P
Instead of all those IF constructs, a SELECT CASE construct would be more efficient since they are mutually exclusive. As for the Trim(), I misconstrued what you are trying to do. It looks like you want the entire line but without the leading spaces. If that is the case, LTrim() as Howard suggested is all you need.
Here is your code with some small changes: Code:
break on $null = SetOption ("NoVarsInStrings","On") $null = SetOption ("Explicit","On") Dim $file,$filehandle,$namevalue,$line,$name,$namearray,$null,$outputfile,$outputhandle $file = 'c:\kix\test_text.txt' $filehandle = FreeFileHandle() If Open ($filehandle,$file) = 0 $namevalue = 0 While @Error = 0 $line = ReadLine ($filehandle) Select Case InStr ($line,'DCS Application Snapshot') ReDim Preserve $namearray[$namevalue] $namearray[$namevalue] = LTrim($line) $namevalue = $namevalue+1 Case InStr ($line,'Authorization') ReDim Preserve $namearray[$namevalue] $namearray[$namevalue] = LTrim($line) $namevalue = $namevalue+1 Case InStr ($line,'Inbound communication address') ReDim Preserve $namearray[$namevalue] $namearray[$namevalue] = LTrim($line) $namevalue = $namevalue+1 Case InStr ($line,'Outbound communication address') ReDim Preserve $namearray[$namevalue] $namearray[$namevalue] = LTrim($line) $namevalue = $namevalue+1 Case InStr ($line,'Application name') ReDim Preserve $namearray[$namevalue] $namearray[$namevalue] = LTrim($line) $namevalue = $namevalue+1 Case InStr ($line,'Application status') ReDim Preserve $namearray[$namevalue] $namearray[$namevalue] = LTrim($line) $namevalue = $namevalue+1 EndSelect Loop $null = Close ($filehandle) $outputfile = 'c:\kix\test_text_out.txt' $outputhandle = FreeFileHandle() If Exist ($outputfile) Del $outputfile EndIf If $namearray[0] <> '' If Open ($outputhandle,$outputfile,5) = 0 For Each $name In $namearray $null = WriteLine ($outputhandle,$name+@CRLF) Next $null = Close ($outputhandle) Else $null = MessageBox ('Unable To Create '+$outputfile,'Error',0) EndIf EndIf Else $null = MessageBox ('Unable To Open '+$file,'Error',0) EndIf
I would rethink your loop however. There is an inherent flaw with looping on @Error=0 when you perform other functions within the loop that can throw @Error. It can end your loop pre-maturely. By readning in the file into an array first and then looping thru the array, it is mitigated.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.
|
|
Top
|
|
|
|
#122587 - 2004-07-12 03:01 PM
Re: Strip leading spaces?
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Not tested ...
I have not verified that
$null = SetOption ("NoVarsInStrings","On")
$null = SetOption ("Explicit","On")
are followed in the older UDFs for this example.
Code:
break on
$null = SetOption ("NoVarsInStrings","On")
$null = SetOption ("Explicit","On")
Dim $file,$filehandle,$namevalue,$line,$name,$namearray,$null,$outputfile,$outputhandle
$file = 'c:\kix\test_text.txt'
$filehandle = FreeFileHandle()
If Open ($filehandle,$file) = 0
$namevalue = 0
Dim $aText, $text, $sMatch
$aText = "DCS Application Snapshot",
"Authorization",
"Inbound communication address",
"Outbound communication address",
"Application name",
"Application status"
$line = ReadLine ($filehandle)
While @Error = 0
for each $text in $aText
$sMatch = "(" + $text + ").*?= (.*?)$"
if RegExpTest($sMatch, $line, 1)
$line = RegExpReplace($line, "^\s+|\s+$", "", 1, 1)
$line = RegExpReplace($line, "\s+=\s+", "=", 1, 1)
Writelog2('c:\kix\test_text_out.txt', $line)
else
? "-No Match found"
endif
Next
$line = ReadLine ($filehandle)
Loop
Endif
;----------- Supporting UDFs ------------
Function RegExpTest($Pattern, $String, $IgnoreCase)
Dim $regEx, $Match, $Matches ; Create variable.
$regEx = createobject("VBscript.RegExp") ; Create a regular expression.
$regEx.Pattern = $Pattern ; Set pattern.
$regEx.IgnoreCase = val($IgnoreCase) ; Set case insensitivity.
$RegExpTest = $regEx.Test($String) ; Execute test search.
EndFunction
Function RegExpReplace($String1, $Pattern, $String2, $IgnoreCase, $Global)
Dim $regEx ; Create variable.
Dim $Results[0]
$regEx = createobject("VBscript.RegExp") ; Create a regular expression.
$regEx.Pattern = $Pattern ; Set pattern.
$regEx.IgnoreCase = val($IgnoreCase) ; Set case insensitivity.
$regEx.Global = val($Global) ; Set global applicability.
$RegExpReplace = $regEx.Replace($String1, $String2) ; Execute search.
EndFunction
Function WriteLog2($File, $Text, optional $TimeStamp)
dim $RC, $FH, $nul
$FH = FreeFileHandle()
if $FH > 0
$RC=Open ($FH, $File, 5)
Select
Case $RC=0
if ($TimeStamp=1)
$TimeStamp = @Date + " " + @Time + " - "
else
$TimeStamp = ""
endif
$RC=Writeline ($FH, $TimeStamp + $Text + @CRLF)
if ($RC <> 0)
$text = "WriteLine error: " + $RC + @CRLF
+ " -4 = File not open for writing" + @CRLF
+ " -3 = File number not open" + @CRLF
+ " -2 = Invalid file number specified" + @CRLF
+ " -1 = End of file"
$nul = MessageBox ($text,"Script Error",48)
endif
$nul=Close ($FH)
exit $RC
Case $RC=-2
$text = "WriteLog2: Invalid file handle (" + $FH + ") specified when trying to Open " + $File
$nul = MessageBox ($text,"Script Error",48)
Case $RC=-1
$text = "WriteLog2: Invalid file name (" + $File + ") specified for log file."
$nul = MessageBox ($text,"Script Error",48)
Case $RC=>0
$text = "System Error(" + $RC + ") while attempting to open log file (" + $File +")."
$nul = MessageBox ($text,"Script Error",48)
Endselect
else
$text = "WriteLog2: No free file handles for opening " + $File
$nul = MessageBox ($text,"Script Error",48)
$RC = -2
endif
exit $RC
EndFunction
Edited by Howard Bullock (2004-07-12 09:11 PM)
|
|
Top
|
|
|
|
#122590 - 2004-07-12 09:12 PM
Re: Strip leading spaces?
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
I have edited the previous post. I found an extra ")" on line 25. I also fixed the two items you listed.
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 484 anonymous users online.
|
|
|