From: Pedro Pujol
Subject: Searching a character within a line
Date: 
Message-ID: <2f5796e7.0111270506.5e46451a@posting.google.com>
Hi kids,

I've got this line:
|2|Dollars|23-dec-1999|Idaho|

And I want to sellect the date (23-dec-1999). If I try with this:
(loop repeat 3 do 
  (setf Position1 (search "|" line)))

I get Position1=0. I would like to 'jump' the first two '|'
characters, in order to get Position1=10. Afterwards, I could use
'subseq' command, but I am not able.

Any suggestion is welcome. Thanx.

From: Kyongho Min
Subject: Re: Searching a character within a line
Date: 
Message-ID: <bd09e78f.0111271357.64b4a857@posting.google.com>
··········@hispavista.com (Pedro Pujol) wrote in message news:<····························@posting.google.com>...
> Hi kids,
> 
> I've got this line:
> |2|Dollars|23-dec-1999|Idaho|
> 
> And I want to sellect the date (23-dec-1999). If I try with this:
> (loop repeat 3 do 
>   (setf Position1 (search "|" line)))
> 
> I get Position1=0. I would like to 'jump' the first two '|'
> characters, in order to get Position1=10. Afterwards, I could use
> 'subseq' command, but I am not able.
> 
> Any suggestion is welcome. Thanx.

Make the line into string and delimit the line by "|" by using
(delimited-string-to-list). And you get the date, if the date position
is fixed.

(delimited-string-to-list line "|") will return
   ("2" "Dollars" "23-dec-1999" "Idaho")

Now you get the answer.

cheers,

Kyongho
From: Janis Dzerins
Subject: Re: Searching a character within a line
Date: 
Message-ID: <87n117hwtw.fsf@asaka.latnet.lv>
···········@aut.ac.nz (Kyongho Min) writes:

> ··········@hispavista.com (Pedro Pujol) wrote in message news:<····························@posting.google.com>...
> > Hi kids,
> > 
> > I've got this line:
> > |2|Dollars|23-dec-1999|Idaho|
> > 
> > And I want to sellect the date (23-dec-1999). If I try with this:
> > (loop repeat 3 do 
> >   (setf Position1 (search "|" line)))
> > 
> > I get Position1=0. I would like to 'jump' the first two '|'
> > characters, in order to get Position1=10. Afterwards, I could use
> > 'subseq' command, but I am not able.
> > 
> > Any suggestion is welcome. Thanx.
> 
> Make the line into string and delimit the line by "|" by using
> (delimited-string-to-list). And you get the date, if the date position
> is fixed.

You should use a function from the standard or a one whose
implementation you also provide, not some implementation specific one.

> (delimited-string-to-list line "|") will return
>    ("2" "Dollars" "23-dec-1999" "Idaho")

... in AllegroCL. (And using #\| instead of "|" would be better,
anyway.)

Not everybody uses ACL and Common Lisp is not defined by any single
implementation of it, but by the standard.

-- 
Janis Dzerins

  Eat shit -- billions of flies can't be wrong.
From: Pierre R. Mai
Subject: Re: Searching a character within a line
Date: 
Message-ID: <87itbwclq5.fsf@orion.bln.pmsf.de>
··········@hispavista.com (Pedro Pujol) writes:

> Hi kids,
> 
> I've got this line:
> |2|Dollars|23-dec-1999|Idaho|
> 
> And I want to sellect the date (23-dec-1999). If I try with this:
> (loop repeat 3 do 
>   (setf Position1 (search "|" line)))
> 
> I get Position1=0. I would like to 'jump' the first two '|'
> characters, in order to get Position1=10. Afterwards, I could use
> 'subseq' command, but I am not able.

A couple of points:

a) Make sure you have established a valid lexical binding for
   variables you set with setf/setq.

b) Use position instead of search, because you are really just looking
   for the position of a single character in a string, not doing a
   substring search, which is more expensive.

c) Take a look at the start keyword parameter to position.

That gets you to

(defun get-field-from-line (field line)
  "Returns a copy of the given field (0 based index) from the line.
Fields are separated by '|'.  If there are fewer fields in line than
field, nil is returned."
  (loop for start = 0 then (1+ position)
        for position = (and start (position #\| line :start start))
        repeat field
        when (null position)
        do (return nil)
        finally
        (return (subseq line start position))))

Regs, Pierre.

-- 
Pierre R. Mai <····@acm.org>                    http://www.pmsf.de/pmai/
 The most likely way for the world to be destroyed, most experts agree,
 is by accident. That's where we come in; we're computer professionals.
 We cause accidents.                           -- Nathaniel Borenstein