Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

string splitting problem

52 views
Skip to first unread message

Robert L.

unread,
Nov 13, 2017, 1:51:23 PM11/13/17
to
Pascal Bourguignon wrote:

> > I have to split a string from /proc/net/arp of the format
> > "127.0.0.1 0x1 0x2 00:11:22:33:AE:5E * ethx"
> > ^^^
> > to extract the MAC.
>
>
> When I don't need the utmost efficient solution, I merely use:
>
> [69]> (split-string (fourth (delete "" (split-string line " ")
> :test (function string=))) ":")
> ("00" "11" "22" "33" "AE" "5E")
>
>
> with (GPL sources):
>
> (DEFUN SPLIT-STRING (STRING &OPTIONAL (SEPARATORS " "))
> "
> NOTE: current implementation only accepts as separators
> a string containing literal characters.
> "
> (UNLESS (SIMPLE-STRING-P STRING) (SETQ STRING (COPY-SEQ STRING)))
> (UNLESS (SIMPLE-STRING-P SEPARATORS) (SETQ SEPARATORS (COPY-SEQ SEPARATORS)))
> (LET ((CHUNKS '())
> (POSITION 0)
> (NEXTPOS 0)
> (STRLEN (LENGTH STRING)) )
> (DECLARE (TYPE SIMPLE-STRING STRING SEPARATORS))
> (LOOP WHILE (< POSITION STRLEN)
> DO
> (LOOP WHILE (AND (< NEXTPOS STRLEN)
> (not (position (CHAR STRING NEXTPOS) separators)))
> DO (SETQ NEXTPOS (1+ NEXTPOS))
> );;loop
> (PUSH (SUBSEQ STRING POSITION NEXTPOS) CHUNKS)
> (SETQ POSITION (1+ NEXTPOS))
> (SETQ NEXTPOS POSITION)
> );;loop
> (NREVERSE CHUNKS)
> );;let
> );;SPLIT-STRING

(define (split-string str (separators " "))
(define seps (string->list separators))
(define strlen (string-length str))
(let loop ((i 0) (anchor 0) (chunks '()))
(cond ((= i strlen) (reverse (cons (substring str anchor i) chunks)))
((member (string-ref str i) seps)
(loop (+ i 1) (+ i 1) (cons (substring str anchor i) chunks)))
(else (loop (+ i 1) anchor chunks)))))

(require srfi/1) ; delete

(define line
"127.0.0.1 0x1 0x2 00:11:22:33:AE:5E * ethx")

(split-string (fourth (delete "" (split-string line))) ":")

===>
("00" "11" "22" "33" "AE" "5E")

In Forth?

--
Despite its proximity and high level of economic development, Israel has
refused to take any Syrian refugees. "We will not allow Israel to be submerged
by a wave of illegal migrants and terrorist activists," Benjamin Netanyahu has
said. http://archive.org/details/nolies

Bob Armstrong

unread,
Nov 13, 2017, 7:47:44 PM11/13/17
to
How's this . In CoSy :
s" 127.0.0.1 0x1 0x2 00:11:22:33:AE:5E * ethx" >t0
t0 "bl VM dae 3 _at s" :" VM
(
00
11
22
33
AE
5E
)


Here are the non-obvious words :

s" : VM " ??
: tokcut ( str tok -- CV ) | cuts string at occurances of string `tok but includes segment before first token
2p LR@ css i0 swap ,I L@ swap cut 2P> ;

: VM : toksplt ( str tok -- CV ) | like ' tokcut but deletes the tokens from the cut pieces
| cr ." toksplt " ( 2p> tokcut i0 R@ rho )
2p LR@ swap cL >aux+> dup R@ css cut | appends
aux- R@ rho ['] cut eachleft 2P> ;

s" : dae " ??
: dae ( v -- v ) >aux+> dup ['] rho 'm ,/ i0 <>i & at aux- ;
| Delete All Empties . Deletes all empty , items of a list .
| translated from K | { x @ & 0 < #:' x } |

--
On Monday, November 13, 2017 at 11:51:23 AM UTC-7, Robert L. wrote:

Ala'a

unread,
Nov 14, 2017, 8:45:15 AM11/14/17
to
Just for fun. This solution is not tested and assumes a couple of things. This
is just for solving the given string sample:

: SPF4 /tmp/mac.f
0 11 22 33 AE 5E


\ mac.f
\ Given buffer address, return address of first letter after SPACE
: 'BL+ ( a - 'bl+) BEGIN DUP C@ BL <> WHILE 1+ REPEAT 1+ ;

\ Given buffer address, read 2 digit number and return next address and number
: ##: ( a1 - a2 u) 0 0 ROT 2 >NUMBER DROP 1+ NIP SWAP ;

\ Given buffer address which contains a line formatted as
\ "nnn.nnn.nnn.nnn 0xn 0xn xx:xx:xx:xx:xx:xx if" print the hardward address
: .MAC ( a - ) 'BL+ 'BL+ 'BL+ ##: . ##: . ##: . ##: . ##: . ##: . DROP ;

S" 127.0.0.1 0x1 0x2 00:11:22:33:AE:5E * ethx" DROP HEX .MAC BYE
0 new messages