READ comma delimited data 
Author Message
 READ comma delimited data

Hi

I've read response from similar questions posted before.  However, I
still haven't made it work.

program main
character*80 par_name
character*80 param(10)
par_name="Data,Data of Jan,Data of Feb ......"    !header
read(par_name,*,end=1) param
1       do i=1,99
                  print *,trim(param(i))
        end do
par_name="1,N/A,2.5"                                     !data
read(par_name,*) i1,param(1),r1
print *,i1,trim(param(1)),r1
stop
end

Results:

Data
Data
of
Jan
Data
of
Feb
......
1      N      0.0000

These results are not what I want. For the header, it should be "Data",
"Data of Jan", "Data of Feb".  Does anyone know how to read these data?
 thank you.

Mike



Fri, 09 Jan 2009 10:34:58 GMT  
 READ comma delimited data


Quote:
> Hi

> I've read response from similar questions posted before.  However, I
> still haven't made it work.

> program main
> character*80 par_name
> character*80 param(10)
> par_name="Data,Data of Jan,Data of Feb ......"    !header
> read(par_name,*,end=1) param

the embedded blanks are acting as list delimiters, either get rid of the
embedded blanks or
get rid of the list format and use a fixed format like:
   read(par_name,'(12(a11,1x))') param

Quote:
> 1 do i=1,99
>                  print *,trim(param(i))
> end do
> par_name="1,N/A,2.5"                                     !data

  the N/A will be delimited by the slash, either get rid of  the / and any
embedded blanks or
 get rid of the list format and use a fixed format..


Fri, 09 Jan 2009 18:57:19 GMT  
 READ comma delimited data

Quote:

> Hi

> I've read response from similar questions posted before.  However, I
> still haven't made it work.

> program main
> character*80 par_name
> character*80 param(10)
> par_name="Data,Data of Jan,Data of Feb ......"    !header
> read(par_name,*,end=1) param
> 1  do i=1,99
>                   print *,trim(param(i))
>    end do
> par_name="1,N/A,2.5"                                     !data
> read(par_name,*) i1,param(1),r1
> print *,i1,trim(param(1)),r1
> stop
> end

> Results:

> Data
> Data
> of
> Jan
> Data
> of
> Feb
> ......
> 1      N      0.0000

> These results are not what I want. For the header, it should be "Data",
> "Data of Jan", "Data of Feb".  Does anyone know how to read these data?
>  thank you.

In addition to David's suggestion, you can also read using a large,
fixed-length string and the appropriate format, use INDEX to locate the
commas, then use substrings to extract out the individual fields.


Sat, 10 Jan 2009 05:16:35 GMT  
 READ comma delimited data

Quote:


> > Hi

> > I've read response from similar questions posted before.  However, I
> > still haven't made it work.

> > program main
> > character*80 par_name
> > character*80 param(10)
> > par_name="Data,Data of Jan,Data of Feb ......"    !header
> > read(par_name,*,end=1) param
> > 1     do i=1,99
> >                   print *,trim(param(i))
> >       end do
> > par_name="1,N/A,2.5"                                     !data
> > read(par_name,*) i1,param(1),r1
> > print *,i1,trim(param(1)),r1
> > stop
> > end

> > Results:

> > Data
> > Data
> > of
> > Jan
> > Data
> > of
> > Feb
> > ......
> > 1      N      0.0000

> > These results are not what I want. For the header, it should be "Data",
> > "Data of Jan", "Data of Feb".  Does anyone know how to read these data?
> >  thank you.

> In addition to David's suggestion, you can also read using a large,
> fixed-length string and the appropriate format, use INDEX to locate the
> commas, then use substrings to extract out the individual fields.

But I can only have position of the first comma and the last comma,
according to the usage of INDEX
              result = INDEX (string, substring [, back] [, kind]) .
Or, I should keep finding the rest of the commas on a iterated process,
such as :
      "1,N/A,2.5,3.5,N/A,N/A,....."
          ^---> from here got another one
                ^-----> from here got another one
                      ^---> from here got another one
I think I can do this.  But the rest of the questions is how to convert
the "2.5" into real numbers.  Are there some intrinsic functions like
STRING2REAL() or STRING2INTEGER()?
Mike


Sat, 10 Jan 2009 08:30:01 GMT  
 READ comma delimited data


Quote:



>> > Hi

>> > I've read response from similar questions posted before.  However, I
>> > still haven't made it work.

>> > program main
>> > character*80 par_name
>> > character*80 param(10)
>> > par_name="Data,Data of Jan,Data of Feb ......"    !header
>> > read(par_name,*,end=1) param
>> > 1 do i=1,99
>> >                   print *,trim(param(i))
>> > end do
>> > par_name="1,N/A,2.5"                                     !data
>> > read(par_name,*) i1,param(1),r1
>> > print *,i1,trim(param(1)),r1
>> > stop
>> > end

>> > Results:

>> > Data
>> > Data
>> > of
>> > Jan
>> > Data
>> > of
>> > Feb
>> > ......
>> > 1      N      0.0000

>> > These results are not what I want. For the header, it should be "Data",
>> > "Data of Jan", "Data of Feb".  Does anyone know how to read these data?
>> >  thank you.

>> In addition to David's suggestion, you can also read using a large,
>> fixed-length string and the appropriate format, use INDEX to locate the
>> commas, then use substrings to extract out the individual fields.
> But I can only have position of the first comma and the last comma,
> according to the usage of INDEX
>              result = INDEX (string, substring [, back] [, kind]) .
> Or, I should keep finding the rest of the commas on a iterated process,
> such as :
>      "1,N/A,2.5,3.5,N/A,N/A,....."
>          ^---> from here got another one
>                ^-----> from here got another one
>                      ^---> from here got another one

Yes method is the one I use.
Quote:
> I think I can do this.  But the rest of the questions is how to convert
> the "2.5" into real numbers.  Are there some intrinsic functions like
> STRING2REAL() or STRING2INTEGER()?
> Mike

You don't need such intrinsics.  Instead use an internal read from a
character variable to a real variable.
Jim

- Show quoted text -



Sat, 10 Jan 2009 09:03:49 GMT  
 READ comma delimited data

Quote:

> But I can only have position of the first comma and the last comma,
> according to the usage of INDEX
>               result = INDEX (string, substring [, back] [, kind]) .
> Or, I should keep finding the rest of the commas on a iterated process,
> such as :
>       "1,N/A,2.5,3.5,N/A,N/A,....."
>           ^---> from here got another one
>                 ^-----> from here got another one
>                       ^---> from here got another one

Yup, that would be the way to do it.  Note that you can call INDEX on a
substring such as

   result = index(inputline(i:j), ',')

where i is the current location, and j is the length of the string.  You
don't need to chop out the remainder of the input line into a separate
variable.

Quote:
> I think I can do this.  But the rest of the questions is how to convert
> the "2.5" into real numbers.  Are there some intrinsic functions like
> STRING2REAL() or STRING2INTEGER()?
> Mike

There are, but they're not exactly implemented as functions.  The
concept is "internal i/o" -- the syntax and behavior is just like an
ordinary READ or WRITE statement, but applied to a string (or, for that
matter, a substring) rather than a file.

Thus, something like this (though I may have off-by-one errors on the
substring indices) should do what you want:

   read(inputline(i:i+result),*) number

Here, "number" is an integer or real variable, as appropriate, and I'm
using a "*" format descriptor for simplicity, but it also works with
explicit format strings too.

- Brooks

--
The "bmoses-nospam" address is valid; no unmunging needed.



Sat, 10 Jan 2009 09:14:47 GMT  
 READ comma delimited data

Quote:


>> But I can only have position of the first comma and the last comma,
>> according to the usage of INDEX
>>               result = INDEX (string, substring [, back] [, kind]) .
>> Or, I should keep finding the rest of the commas on a iterated process,
>> such as :
>>       "1,N/A,2.5,3.5,N/A,N/A,....."
>>           ^---> from here got another one
>>                 ^-----> from here got another one
>>                       ^---> from here got another one

> Yup, that would be the way to do it.  Note that you can call INDEX on a
> substring such as

>   result = index(inputline(i:j), ',')

> where i is the current location, and j is the length of the string.  You
> don't need to chop out the remainder of the input line into a separate
> variable.

>> I think I can do this.  But the rest of the questions is how to convert
>> the "2.5" into real numbers.  Are there some intrinsic functions like
>> STRING2REAL() or STRING2INTEGER()?
>> Mike

> There are, but they're not exactly implemented as functions.  The
> concept is "internal i/o" -- the syntax and behavior is just like an
> ordinary READ or WRITE statement, but applied to a string (or, for that
> matter, a substring) rather than a file.

> Thus, something like this (though I may have off-by-one errors on the
> substring indices) should do what you want:

>   read(inputline(i:i+result),*) number

> Here, "number" is an integer or real variable, as appropriate, and I'm
> using a "*" format descriptor for simplicity, but it also works with
> explicit format strings too.

> - Brooks

But this is such a common parsing task.  Aren't there standard libraries
for doing this say with a template to provide the valid values,
positions, keywords, value types, etc.  You could do an awful lot with a
not too sophisticated set of routines.  Just pass a text string and get
back a list of key words and associated values, and/or positional
values.  I'm sure I've written such in the distant past.

--

Gary Scott

fortran Library:  http://www.fortranlib.com

Support the Original G95 Project:  http://www.g95.org
-OR-
Support the GNU GFortran Project:  http://gcc.gnu.org/fortran/index.html

Why are there two?  God only knows.

If you want to do the impossible, don't hire an expert because he knows
it can't be done.

-- Henry Ford



Sat, 10 Jan 2009 09:38:19 GMT  
 READ comma delimited data
Gary Scott

Quote:


> >> But I can only have position of the first comma and the last comma,
> >> according to the usage of INDEX
> >>               result = INDEX (string, substring [, back] [, kind]) .
> >> Or, I should keep finding the rest of the commas on a iterated process,
> >> such as :
> >>       "1,N/A,2.5,3.5,N/A,N/A,....."
> >>           ^---> from here got another one
> >>                 ^-----> from here got another one
> >>                       ^---> from here got another one

> > Yup, that would be the way to do it.  Note that you can call INDEX on a
> > substring such as

> >   result = index(inputline(i:j), ',')

> > where i is the current location, and j is the length of the string.  You
> > don't need to chop out the remainder of the input line into a separate
> > variable.

> >> I think I can do this.  But the rest of the questions is how to convert
> >> the "2.5" into real numbers.  Are there some intrinsic functions like
> >> STRING2REAL() or STRING2INTEGER()?
> >> Mike

> > There are, but they're not exactly implemented as functions.  The
> > concept is "internal i/o" -- the syntax and behavior is just like an
> > ordinary READ or WRITE statement, but applied to a string (or, for that
> > matter, a substring) rather than a file.

> > Thus, something like this (though I may have off-by-one errors on the
> > substring indices) should do what you want:

> >   read(inputline(i:i+result),*) number

> > Here, "number" is an integer or real variable, as appropriate, and I'm
> > using a "*" format descriptor for simplicity, but it also works with
> > explicit format strings too.

> > - Brooks

> But this is such a common parsing task.  Aren't there standard libraries
> for doing this say with a template to provide the valid values,
> positions, keywords, value types, etc.  You could do an awful lot with a
> not too sophisticated set of routines.  Just pass a text string and get
> back a list of key words and associated values, and/or positional
> values.  I'm sure I've written such in the distant past.

> --

> Gary Scott

> Fortran Library:  http://www.fortranlib.com

> Support the Original G95 Project:  http://www.g95.org
> -OR-
> Support the GNU GFortran Project:  http://gcc.gnu.org/fortran/index.html

> Why are there two?  God only knows.

> If you want to do the impossible, don't hire an expert because he knows
> it can't be done.

> -- Henry Ford

If it's written as a module with func/sub contained, that will be
easily used. As you know, just USE it.
I think if this can be done in the internal read, that'll be wonderful.
 However, as I show in the question , it also separates by space in
addition to comma.  It is a pity.
Mike
Mike


Sat, 10 Jan 2009 09:55:51 GMT  
 READ comma delimited data

Mike

Quote:
> Gary Scott



> > >> But I can only have position of the first comma and the last comma,
> > >> according to the usage of INDEX
> > >>               result = INDEX (string, substring [, back] [, kind]) .
> > >> Or, I should keep finding the rest of the commas on a iterated process,
> > >> such as :
> > >>       "1,N/A,2.5,3.5,N/A,N/A,....."
> > >>           ^---> from here got another one
> > >>                 ^-----> from here got another one
> > >>                       ^---> from here got another one

> > > Yup, that would be the way to do it.  Note that you can call INDEX on a
> > > substring such as

> > >   result = index(inputline(i:j), ',')

> > > where i is the current location, and j is the length of the string.  You
> > > don't need to chop out the remainder of the input line into a separate
> > > variable.

> > >> I think I can do this.  But the rest of the questions is how to convert
> > >> the "2.5" into real numbers.  Are there some intrinsic functions like
> > >> STRING2REAL() or STRING2INTEGER()?
> > >> Mike

> > > There are, but they're not exactly implemented as functions.  The
> > > concept is "internal i/o" -- the syntax and behavior is just like an
> > > ordinary READ or WRITE statement, but applied to a string (or, for that
> > > matter, a substring) rather than a file.

> > > Thus, something like this (though I may have off-by-one errors on the
> > > substring indices) should do what you want:

> > >   read(inputline(i:i+result),*) number

> > > Here, "number" is an integer or real variable, as appropriate, and I'm
> > > using a "*" format descriptor for simplicity, but it also works with
> > > explicit format strings too.

> > > - Brooks

> > But this is such a common parsing task.  Aren't there standard libraries
> > for doing this say with a template to provide the valid values,
> > positions, keywords, value types, etc.  You could do an awful lot with a
> > not too sophisticated set of routines.  Just pass a text string and get
> > back a list of key words and associated values, and/or positional
> > values.  I'm sure I've written such in the distant past.

> > --

> > Gary Scott

> > Fortran Library:  http://www.fortranlib.com

> > Support the Original G95 Project:  http://www.g95.org
> > -OR-
> > Support the GNU GFortran Project:  http://gcc.gnu.org/fortran/index.html

> > Why are there two?  God only knows.

> > If you want to do the impossible, don't hire an expert because he knows
> > it can't be done.

> > -- Henry Ford
> If it's written as a module with func/sub contained, that will be
> easily used. As you know, just USE it.
> I think if this can be done in the internal read, that'll be wonderful.
>  However, as I show in the question , it also separates by space in
> addition to comma.  It is a pity.
> Mike
> Mike

Sorry, as also denoted by David,  "Internal read" also separates by
slash "/" which is actually not liked.
Mike


Sat, 10 Jan 2009 10:10:42 GMT  
 READ comma delimited data

(snip)

Quote:
> But this is such a common parsing task.  Aren't there standard libraries
> for doing this say with a template to provide the valid values,
> positions, keywords, value types, etc.  You could do an awful lot with a
> not too sophisticated set of routines.  Just pass a text string and get
> back a list of key words and associated values, and/or positional
> values.  I'm sure I've written such in the distant past.

That sounds like NAMELIST.  I don't see any rule against using
NAMELIST with internal I/O.  A multiple record internal I/O would
allow one to add the namelist name and termination to the supplied
data before doing a NAMELIST read.

-- glen



Sat, 10 Jan 2009 12:21:40 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. getting fields NOT comma delimited with commas inside

2. Parsing Comma Delimited Data

3. Reading Comma delimited, Quoted String records

4. VW code to read comma-delimited text files??

5. Fortran 77: read second number on a line in a comma delimited text file

6. Reading a comma delimited file

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

8. Reading complex data with DECIMAL='COMMA'

9. Reading in data from a delimited file

10. Parsing Comma delimited files in J

11. Convert comma-delimited records to fixed length records

12. matching records in a comma delimited file

 

 
Powered by phpBB® Forum Software