the car of a string O:-) 
Author Message
 the car of a string O:-)

Hi!

        I'm reading lines from an input file (via read-line) that look
like this:

ABOUT          102    24    1    1

and I'm getting the string "ABOUT          102    24    1    1".

I need to access separatly the "car" (ABOUT) and the "cdr" (102 24 1
1).  What's the easiest way to do it? O:-)

//-----------------------------------------------
//      Fernando Rodriguez Romero
//
//      frr at mindless dot com
//------------------------------------------------



Thu, 11 Apr 2002 03:00:00 GMT  
 the car of a string O:-)
Fernando asks:

Quote:
> Hi!

> I'm reading lines from an input file (via read-line) that look
> like this:

> ABOUT          102    24    1    1

> and I'm getting the string "ABOUT          102    24    1    1".

> I need to access separatly the "car" (ABOUT) and the "cdr" (102 24 1
> 1).  What's the easiest way to do it? O:-)

If your input does not contain anything that looks like an S-expression,
then
the easiest way I can think of is to define a string port over your input
string
and then use (read string-port) to pull off the individual tokens.  If you
really
want to decompose this into a "car" and "cdr",  you could use the read from
string port to get the "car" and then use another read-line from the string
port
to get the "cdr".

Something like:

    (define (string-car the-string)
      (read (open-input-string the-string)))

    (define (string-cdr the-string)
      (let ((the-port (open-input-string the-string)))
        (read the-port)
        (read-line the-port)))

(This is a quicky.  You might want to think about what you want to do if you
get
an eof object on a read.)

David Albertz



Thu, 11 Apr 2002 03:00:00 GMT  
 the car of a string O:-)

 > Fernando asks:
 >
 > > Hi!
 > >
 > > I'm reading lines from an input file (via read-line) that look
 > > like this:
 > >
 > > ABOUT          102    24    1    1
 > >
 > > and I'm getting the string "ABOUT          102    24    1    1".
 > >
 > > I need to access separatly the "car" (ABOUT) and the "cdr" (102 24 1
 > > 1).  What's the easiest way to do it? O:-)
 >
 > If your input does not contain anything that looks like an
 > S-expression, then the easiest way I can think of is to define a
 > string port over your input string and then use (read string-port)
 > to pull off the individual tokens.  If you really want to decompose
 > this into a "car" and "cdr", you could use the read from string
 > port to get the "car" and then use another read-line from the
 > string port to get the "cdr".

It is way easier to read the string (string-append "(" the-line ")").
--
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
         http://www.cs.cornell.edu/eli/meaning.html        Maze is Life!



Thu, 11 Apr 2002 03:00:00 GMT  
 the car of a string O:-)


Quote:
>    I'm reading lines from an input file (via read-line) that look
>like this:

>ABOUT          102    24    1    1

>and I'm getting the string "ABOUT          102    24    1    1".

>I need to access separatly the "car" (ABOUT) and the "cdr" (102 24 1
>1).  What's the easiest way to do it? O:-)

You will actually need to parse it out into tokens, possibly based
on spaces.

There's not a built-in operator in Scheme to do this; the behaviour
that you suggest is arguably not terribly appropriate.

If you're splitting on whitespace, then the likely result of
(parse string) for the string you describe might be more like:

   (ABOUT #f #f #f #f #f #f 102 #f #f 24 #f #f 1 #f #f 1)

where you'd filter out the #f's to get the desired list.

More particularly, you may have here a set of records of fixed width
where you might have a "parser" that describes the structure of the data
thus:
(define
   field-structure
     '((symbol 12) (number 8) (number 8) (number 8) (number 8)))
where you'd then do something like
(define record-contents
   (parse-line linestring field-structure))
to get the desired list.

The function parse-line would grab sections of the string, evaluate them
assortedly as symbols, numbers, strings, or otherwise based on the
contents of field-structure.
--
LISP car-and-cdr worlds are a more reasonable representation of the things
that make life interesting than fixed decimal(15) or FILE OLDMSTR RECORD IS
PAYROLL.
-- Bernie Greenberg.



Thu, 11 Apr 2002 03:00:00 GMT  
 the car of a string O:-)

Quote:
>    I'm reading lines from an input file (via read-line) that look
>like this:

>ABOUT          102    24    1    1

>and I'm getting the string "ABOUT          102    24    1    1".

>I need to access separatly the "car" (ABOUT) and the "cdr" (102 24 1
>1).  What's the easiest way to do it? O:-)

You will actually need to parse it out into tokens, possibly based
on spaces.

There's not a built-in operator in Scheme to do this; the behaviour
that you suggest is arguably not terribly appropriate.

If you're splitting on whitespace, then the likely result of
(parse string) for the string you describe might be more like:

   (ABOUT #f #f #f #f #f #f 102 #f #f 24 #f #f 1 #f #f 1)

where you'd filter out the #f's to get the desired list.

More particularly, you may have here a set of records of fixed width
where you might have a "parser" that describes the structure of the data
thus:
(define
   field-structure
     '((symbol 12) (number 8) (number 8) (number 8) (number 8)))
where you'd then do something like
(define record-contents
   (parse-line linestring field-structure))
to get the desired list.

The function parse-line would grab sections of the string, evaluate them
assortedly as symbols, numbers, strings, or otherwise based on the
contents of field-structure.
--
LISP car-and-cdr worlds are a more reasonable representation of the things
that make life interesting than fixed decimal(15) or FILE OLDMSTR RECORD IS
PAYROLL.
-- Bernie Greenberg.



Thu, 11 Apr 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Why is (eq? car car) false in MacScheme

2. Method to print a string directly to LPT1 in VA 3.0 OS/2

3. Help - Method to print a string directly to LPT1 in VAST 3.0 OS/2

4. OS/2 REXX SysCreateObject() WinCreateObject setup string tips?

5. OS/2 Querying setup strings on Directories?

6. OS/2, setup string in syssetobjectdata function

7. Svar: [OS/2] Sys*object* setup strings question

8. Passing string to OS/2 command line.

9. Passing string to OS/2 command line.

10. Need setup string for WPDataFile in OS/2

11. os._exit(), os.kill(os.getpid(), 9)

12. Why does (eq? car car) ==> () in MacScheme?

 

 
Powered by phpBB® Forum Software