Parsing Comma Delimited Data 
Author Message
 Parsing Comma Delimited Data

CW55EEABC

What is the best way to parse the following:

LOC:String = 'ITEM1,ITEM2,ITEM3,ITEM4,ITEM5'

LOC:1 = ITEM1 (First Item in LOC:String)
LOC:2 = ITEM2 (Second Item in LOC:String)
....
....
LOC:5 = ITEM5 (Fifth Item in LOC:String)

TIA

Tim Morrison



Tue, 22 Nov 2005 00:20:09 GMT  
 Parsing Comma Delimited Data
Tim

try

============================================
  t#=instring(',',myfield,1,1)
  idx#=1
  loop while t#
    execute(idx#)
      field1=sub(myfield,1,t#)
      field2=sub(myfield,1,t#)
      field3=sub(myfield,1,t#)
      field4=sub(myfield,1,t#)
      field5=sub(myfield,1,t#)
      STOP('TOO MANY FIELDS'
    .
    MYFIELD=SUB(MYFIELD,T#+1,SIZE(MYFIELD))
    IDX#+=1
  END
  execute(idx#)
    field1=myfield
    field2=myfield
    field3=myfield
    field4=myfield
    field5=myfield
    STOP('TOO MANY FIELDS')
  .

============================================

Cheers



Tue, 22 Nov 2005 00:37:20 GMT  
 Parsing Comma Delimited Data
Sorry I forgot to add at the end of the Loop "t#=instring(',',myfield,1,1)"
to refresh the location of the next comma

Also this is assuming there are no spurious commas within the data you want
to parse

Cheers

--
Ian Holdsworth
Senior Programmer / Assistant IT Manager
Response Analysis & Mailing Ltd

---------------------------------------------------

Direct Line:       +44 (0) 20 8880 8866
Switch Board:      +44 (0) 20 8880 8140
Fax:               +44 (0) 0870 134 0987

Web Site:          www.ram-ltd.co.uk



Quote:
> Tim

> try

> ============================================
>   t#=instring(',',myfield,1,1)
>   idx#=1
>   loop while t#
>     execute(idx#)
>       field1=sub(myfield,1,t#)
>       field2=sub(myfield,1,t#)
>       field3=sub(myfield,1,t#)
>       field4=sub(myfield,1,t#)
>       field5=sub(myfield,1,t#)
>       STOP('TOO MANY FIELDS'
>     .
>     MYFIELD=SUB(MYFIELD,T#+1,SIZE(MYFIELD))
>     IDX#+=1
>   END
>   execute(idx#)
>     field1=myfield
>     field2=myfield
>     field3=myfield
>     field4=myfield
>     field5=myfield
>     STOP('TOO MANY FIELDS')
>   .

> ============================================

> Cheers



Tue, 22 Nov 2005 00:40:45 GMT  
 Parsing Comma Delimited Data

Hi Tim,

the following procedure parses a string for a set of
separators ("|" if no separator defined)

!-------------------------------------------------------------------------------------------
getItem   PROCEDURE(STRING Items, BYTE ItemNr, <STRING _Separators>)
!-------------------------------------------------------------------------------------------
Separators  CSTRING(32)
Sep         LONG(1)
i           LONG
Start       LONG
Stop        LONG
Cnt         LONG(1)

  CODE
    IF ItemNr=0 THEN
      RETURN ''
    END

    IF ~omitted(3) THEN
      Separators=_Separators
    ELSE
      Separators='|'
    END
    IF ItemNr=1 THEN
      Start=1
    END
    LOOP i=1 TO len(clip(Items))
      IF instring(Items[i],Separators) THEN
        IF Start=0 THEN
          IF Cnt<ItemNr-1 THEN
            Cnt+=1
            CYCLE
          ELSE
            Start=i+1
          END
        ELSE
          Stop=i-1
          BREAK
        END
      END
    END
    IF Stop=0 THEN
      Stop=len(clip(Items))
    END

    RETURN sub(Items,Start,Stop-Start+1)

--

with best regards,

----------------------------------------------------------------------------
i.Con Informatik.Consulting  -  Dipl.-Inform. Jens Eden
Turngartenstr. 24b  -  26160 Bad Zwischenahn - Germany
----------------------------------------------------------------------------
the TPS.repair Templates - forget about TPS file corruptions
30 days trial version at :  http://www.informatik-consulting.de/DL
----------------------------------------------------------------------------


Quote:
> CW55EEABC

> What is the best way to parse the following:

> LOC:String = 'ITEM1,ITEM2,ITEM3,ITEM4,ITEM5'

> LOC:1 = ITEM1 (First Item in LOC:String)
> LOC:2 = ITEM2 (Second Item in LOC:String)
> ....
> ....
> LOC:5 = ITEM5 (Fifth Item in LOC:String)

> TIA

> Tim Morrison



Tue, 22 Nov 2005 01:00:36 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Parsing Comma delimited files in J

2. parsing a comma delimited string

3. newbie needs a little help parsing a comma delimited file

4. getting fields NOT comma delimited with commas inside

5. READ comma delimited data

6. FS is a comma, but comma in data!

7. Convert comma-delimited records to fixed length records

8. matching records in a comma delimited file

9. comma delimited

10. Comma delimited file problem

11. AWK & Comma Delimited Text

12. Reading Comma delimited, Quoted String records

 

 
Powered by phpBB® Forum Software