Page 1 of 1 1
Topic Options
#8135 - 2001-04-17 02:29 PM Looking for tabs
Anonymous
Unregistered


I have a file that conatins data in the form
<data1> (tab) <data2> (tab) <data3>
The data in the middle field is IP addresses and so I cannot read the data with the substr command as they are different lengths. Is anyone aware of a way to strip the information by searching for the data between the tabs. I think the pseudocode for this would be something like
$data1 = (anything from start of line to tab)
$data2 = (data between tab1 + tab2)
$data3 = (anything from tab2 to the end of the line)

Thanks a lot

Chris

Top
#8136 - 2001-04-17 03:01 PM Re: Looking for tabs
Anonymous
Unregistered


Try using the instr() function to search for chr(9), the tab character. I haven't tested this but try something like:
code:

$temp=substr("$data1",instr("$data1",chr(9))+1,999)
$ptr=instr("$temp",chr(9))
$data3=substr("$temp",$ptr+1,999)
$data2=substr("$temp",1,$ptr-1)


Top
#8137 - 2001-04-17 03:04 PM Re: Looking for tabs
Anonymous
Unregistered


Oops. I used $data1 as the starting point instead of evaluating it but I'm sure you get the idea.
Top
#8138 - 2001-04-17 03:04 PM Re: Looking for tabs
cj Offline
MM club member
*****

Registered: 2000-04-06
Posts: 1102
Loc: Brisbane, Australia
TAB is CHR(9)

INSTR($text, chr(9)) will return the first occurrence of TAB in $text.

If you want to remove all the TABS by converting them to space try this:

piece of Swap.KiX

code:

$changefrom=chr(9)
$changeto=" "


while instr("$string", $changefrom)<>0
$string=substr($string, 1, instr("$string", $changefrom)-1) + $changeto + substr($string, instr("$string", $changefrom)+1, len($string))
loop


$string is your text
$changefrom is TAB
$changeto is SPACE

This will continue looking at string for TAB until there are none. If it finds a TAB, however, it uses the SUBSTR and INSTR functions to break the string apart and re-join it with the TAB missing and replaced with a SPACE.

If you just want to strip out the TABs, make $changeto=""

If you want to separate the pieces out without the TABs, you have two choices.

1. Use the SPLIT function in the new KiXtart 2001 Beta.

2. Use cjs VBSplit.KiX script that works in KiXtart 3.6x and does the same as the new KiX2k function.

You can get VBSplit.KiX from here


cj

Top
#8139 - 2001-04-17 03:52 PM Re: Looking for tabs
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6372
Loc: Stuttgart, Germany
Hiya Chris,

i can't resist to post my result (costs that much time !) alltough that vbsplit might be much better ..

so here's my sample text input :

code:

Machine1 10.139.123.20 Windows NT
Machine2 53.113.5.67 Windows 95
Machine3 10.128.12.15 Windows 2000


and here comes the script :

code:

Break ON CLS

$file = "PATH\test.txt"

DIM $name[3]
DIM $ip[3]
DIM $os[3]
$y = 0

$nul = open(1,$file,2)
$x = readline(1)
WHILE @error = 0
$name[$y] = substr($x,1,instr($x,chr(9))-1)
$x = substr($x,len($name[$y])+2,len($x)-instr($x,chr(9)))
$ip[$y] = substr($x,1,instr($x,chr(9))-1)
$os[$y] = substr($x,instr($x,chr(9))+1,len($x)-instr($x,chr(9)))
at($y,1)"-" + $name[$y] + "-"
at($y,25)"-" + $ip[$y] + "-"
at($y,50)"-" + $os[$y] + "-"
$x = readline(1)
$y = $y + 1
LOOP

$nul = close(1)


Get $k



Jochen

[This message has been edited by jpols (edited 17 April 2001).]

_________________________



Top
#8140 - 2001-04-17 04:13 PM Re: Looking for tabs
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6372
Loc: Stuttgart, Germany
... or much better , make the array independant from the number of Lines :

code:

Break ON CLS
$file = "PATH\test.txt"
$nul = open(1,$file,2)
$x = readline(1)
while @error = 0
$y = $y + 1
$x = readline(1)
loop
$nul = close(1)
DIM $name[$y]
DIM $ip[$y]
DIM $os[$y]
$y = 0
$nul = open(1,$file,2)
$x = readline(1)
WHILE @error = 0
$name[$y] = substr($x,1,instr($x,chr(9))-1)
$x = substr($x,len($name[$y])+2,len($x)-instr($x,chr(9)))
$ip[$y] = substr($x,1,instr($x,chr(9))-1)
$os[$y] = substr($x,instr($x,chr(9))+1,len($x)-instr($x,chr(9)))
at($y,1)"-" + $name[$y] + "-"
at($y,25)"-" + $ip[$y] + "-"
at($y,50)"-" + $os[$y] + "-"
$x = readline(1)
$y = $y + 1
LOOP
$nul = close(1)
Get $k


Jochen

[This message has been edited by jpols (edited 18 April 2001).]

_________________________



Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 167 anonymous users online.
Newest Members
gkustra, emnipetro, Hirze, thequeen, ameliaryan
17642 Registered Users

Generated in 0.029 seconds in which 0.011 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org