how to read line by line from a file through a OCEAN script

S

Samiran Dam

Guest
Dear all,

I need to read from a file in A OCEAN script. The situation is - the
file stores the data as following:

1 2 3 4
5 6 7 8
9 10 11 12
....

That is in each iteration I have to pick up one line from the file and
run simulation. Each of the number in a line describes the value of a
desVar.

How I can do this....I am not able to do this using gets &
fscanf....Please help.

regards,
Samiran
 
On Aug 29, 7:37 pm, Samiran Dam <samiran.iit...@gmail.com> wrote:
Dear all,

I need to read from a file in A OCEAN script. The situation is - the
file stores the data as following:

1  2   3   4
5  6   7   8
9 10 11 12
...

That is in each iteration I have to pick up one line from the file and
run simulation. Each of the number in a line describes the value of a
desVar.

How I can do this....I am not able to do this using gets &
fscanf....Please help.

regards,
Samiran
Hi there,

What's the problem with "gets" ?
I've just try a small example:
~~~~~~~~~~~~~~~~~~~~~
/*
Read a file with content like:
1 2 3 4
5 6 7 8
9 10 11 12

and return a list of lists, like
list( list(1 2 3 4)
list(5 6 7 8)
list(9 10 11 12)
)

*/
procedure( My_readDataFile(fileName)
let( (pIn str retList)
pIn = infile(fileName)
while(gets(str pIn)
retList = cons(parseString(str) retList)
)
close(pIn)
reverse(retList) ;; we have retList in reverse order
)
)

My_x = My_readDataFile("/home/predamar/test.in")
~~~~~~~~~~~~~~~~~~~~~~

And it works just perfect

BR,
Marcel
 
On Aug 30, 12:24 pm, Marcel Preda <marcel.pr...@gmail.com> wrote:
On Aug 29, 7:37 pm, Samiran Dam <samiran.iit...@gmail.com> wrote:



Dear all,

I need to read from a file in A OCEAN script. The situation is - the
file stores the data as following:

1  2   3   4
5  6   7   8
9 10 11 12
...

That is in each iteration I have to pick up one line from the file and
run simulation. Each of the number in a line describes the value of a
desVar.

How I can do this....I am not able to do this using gets &
fscanf....Please help.

regards,
Samiran

Hi there,

What's the problem with "gets" ?
I've just try a small example:
~~~~~~~~~~~~~~~~~~~~~
/*
Read a file with content like:
    1  2   3   4
    5  6   7   8
    9 10 11 12

and return a list of lists, like
    list( list(1 2 3 4)
        list(5 6 7 8)
        list(9 10 11 12)
    )

*/
procedure( My_readDataFile(fileName)
let( (pIn str retList)
    pIn = infile(fileName)
    while(gets(str pIn)
        retList = cons(parseString(str) retList)
    )
    close(pIn)
    reverse(retList) ;; we have retList in reverse order
)
)

My_x = My_readDataFile("/home/predamar/test.in")
~~~~~~~~~~~~~~~~~~~~~~

And it works just perfect

BR,
Marcel
Hi,

Thanks for the reply.

I have tried the code you provided as follows:

"test.ocn"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fout=outfile("/home/samiran/Desktop/output.txt")

let( (pIn str retList)
pIn = infile("/home/samiran/Desktop/input.txt")
while(gets(str pIn)
retList = cons(parseString(str) retList)
)
close(pIn)
reverse(retList) ;; we have retList in reverse order

foreach(list1 retList
foreach(num list1
fprintf(fout "%g " num)
)
)

)

newline(fout)
close(fout)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

*Error* fprintf/sprintf: format spec. incompatible with data - "9"
*Error* load: error while loading file - "/home/samiran/Desktop/
test.ocn"


Could you please look into this?


Thanks & Regards,
Samiran
 
On Aug 31, 7:55 am, Samiran Dam <samiran.iit...@gmail.com> wrote:
On Aug 30, 12:24 pm, Marcel Preda <marcel.pr...@gmail.com> wrote:



On Aug 29, 7:37 pm, Samiran Dam <samiran.iit...@gmail.com> wrote:

Dear all,

I need to read from a file in A OCEAN script. The situation is - the
file stores the data as following:

1  2   3   4
5  6   7   8
9 10 11 12
...

That is in each iteration I have to pick up one line from the file and
run simulation. Each of the number in a line describes the value of a
desVar.

How I can do this....I am not able to do this using gets &
fscanf....Please help.

regards,
Samiran

Hi there,

What's the problem with "gets" ?
I've just try a small example:
~~~~~~~~~~~~~~~~~~~~~
/*
Read a file with content like:
    1  2   3   4
    5  6   7   8
    9 10 11 12

and return a list of lists, like
    list( list(1 2 3 4)
        list(5 6 7 8)
        list(9 10 11 12)
    )

*/
procedure( My_readDataFile(fileName)
let( (pIn str retList)
    pIn = infile(fileName)
    while(gets(str pIn)
        retList = cons(parseString(str) retList)
    )
    close(pIn)
    reverse(retList) ;; we have retList in reverse order
)
)

My_x = My_readDataFile("/home/predamar/test.in")
~~~~~~~~~~~~~~~~~~~~~~

And it works just perfect

BR,
Marcel

Hi,

Thanks for the reply.

I have tried the code you provided as follows:

"test.ocn"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fout=outfile("/home/samiran/Desktop/output.txt")

let( (pIn str retList)
    pIn = infile("/home/samiran/Desktop/input.txt")
    while(gets(str pIn)
        retList = cons(parseString(str) retList)
    )
    close(pIn)
    reverse(retList) ;; we have retList in reverse order

    foreach(list1 retList
        foreach(num list1
                fprintf(fout "%g " num)
        )
    )

)

newline(fout)
close(fout)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

*Error* fprintf/sprintf: format spec. incompatible with data - "9"
*Error* load: error while loading file - "/home/samiran/Desktop/
test.ocn"

Could you please look into this?

Thanks & Regards,
Samiran
Hi Samiran,

In the lists each element is a string, they were not yet "converted"
to numbers.
And you have used "%g" format, it is for floating numbers.
Use
fprintf(fout "%s " num)

BR,
Marcel
 
Hi Samiran,

In the lists each element is a string, they were not yet "converted"
to numbers.
And you have used "%g" format, it is for floating numbers.
Use
fprintf(fout "%s " num)

BR,
Marcel
You could always use lineread() instead of gets() to read each line from the
file. This will read them using the SKILL parser. So if you do:

procedure( My_readDataFile(fileName)
let( (pIn data retList)
pIn = infile(fileName)
while(data=lineread(pIn)
unless(data==t
retList=tconc(retList data)
)
)
close(pIn)
car(retList)
)
)

Because I used lineread, the numbers get read as integers. Of course, if you
then tried to print them using %g, you'd still have a problem, because integers
are not floats (could use %n instead though).

I also used tconc to avoid reversing the list.

Regards,

Andrew.
 
On Sep 1, 7:38 pm, Andrew Beckett <andr...@DcEaLdEeTnEcTe.HcIoSm>
wrote:
Hi Samiran,

In the lists each element is a string, they were not yet "converted"
to numbers.
And you have used "%g" format, it is for floating numbers.
Use
fprintf(fout "%s " num)

BR,
Marcel

You could always use lineread() instead of gets() to read each line from the
file. This will read them using the SKILL parser. So if you do:

procedure( My_readDataFile(fileName)
let( (pIn data retList)
   pIn = infile(fileName)
   while(data=lineread(pIn)
     unless(data==t
       retList=tconc(retList data)
     )
   )
   close(pIn)
   car(retList)
)
)

Because I used lineread, the numbers get read as integers. Of course, if you
then tried to print them using %g, you'd still have a problem, because integers
are not floats (could use %n instead though).

I also used tconc to avoid reversing the list.

Regards,

Andrew.
Hi,

In my situation, the numbers are not necessarily be integers..it can
also be floating point numbers. Is there any method available to
convert string to floats?

Andrew,

What is "car(retList)" processing?

I am trying to use your code (....input data are integers only) in
following manner:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fout=outfile("/home/samiran/Desktop/output.txt")

let((pIn data retList)
pIn = infile("/home/samiran/Desktop/input.txt")
while(data=lineread(pIn)
unless(data==t
retList=tconc(retList data)
)
)
close(pIn)
car(retList)

foreach(list1 retList
foreach(num list1
fprintf(fout "%n " num)
)
newline(fout)
)
)

newline(fout)
close(fout)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The error is:

*Error* fprintf/sprintf: format spec. incompatible with data - (1 2 3
4)



BR
Samiran
 
On Sep 2, 9:42 am, Samiran <samiran....@gmail.com> wrote:
On Sep 1, 7:38 pm, Andrew Beckett <andr...@DcEaLdEeTnEcTe.HcIoSm
wrote:



Hi Samiran,

In the lists each element is a string, they were not yet "converted"
to numbers.
And you have used "%g" format, it is for floating numbers.
Use
fprintf(fout "%s " num)

BR,
Marcel

You could always use lineread() instead of gets() to read each line from the
file. This will read them using the SKILL parser. So if you do:

procedure( My_readDataFile(fileName)
let( (pIn data retList)
   pIn = infile(fileName)
   while(data=lineread(pIn)
     unless(data==t
       retList=tconc(retList data)
     )
   )
   close(pIn)
   car(retList)
)
)

Because I used lineread, the numbers get read as integers. Of course, if you
then tried to print them using %g, you'd still have a problem, because integers
are not floats (could use %n instead though).

I also used tconc to avoid reversing the list.

Regards,

Andrew.

Hi,

In my situation, the numbers are not necessarily be integers..it can
also be floating point numbers. Is there any method available to
convert string to floats?

Andrew,

What is "car(retList)" processing?

I am trying to use your code (....input data are integers only) in
following manner:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fout=outfile("/home/samiran/Desktop/output.txt")

let((pIn data retList)
        pIn = infile("/home/samiran/Desktop/input.txt")
        while(data=lineread(pIn)
                unless(data==t
                       retList=tconc(retList data)
                )
        )
        close(pIn)
        car(retList)

        foreach(list1 retList
                foreach(num list1
                        fprintf(fout "%n " num)
                )
                newline(fout)
        )
)

newline(fout)
close(fout)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The error is:

*Error* fprintf/sprintf: format spec. incompatible with data - (1 2 3
4)

BR
Samiran
How can I access a list element-by-element? I mean can I convert a
list to an array? Using foreach(...) gives me the element values to
the same variable, but I want like this - suppose if a list contains N
elements, then I want 1st element to be stored into 'var1', 2nd to be
stored into 'var2' and so on.


BR
Samiran
 
On Sep 2, 11:44 am, Samiran <samiran....@gmail.com> wrote:
On Sep 2, 9:42 am, Samiran <samiran....@gmail.com> wrote:



On Sep 1, 7:38 pm, Andrew Beckett <andr...@DcEaLdEeTnEcTe.HcIoSm
wrote:

Hi Samiran,

In the lists each element is a string, they were not yet "converted"
to numbers.
And you have used "%g" format, it is for floating numbers.
Use
fprintf(fout "%s " num)

BR,
Marcel

You could always use lineread() instead of gets() to read each line from the
file. This will read them using the SKILL parser. So if you do:

procedure( My_readDataFile(fileName)
let( (pIn data retList)
   pIn = infile(fileName)
   while(data=lineread(pIn)
     unless(data==t
       retList=tconc(retList data)
     )
   )
   close(pIn)
   car(retList)
)
)

Because I used lineread, the numbers get read as integers. Of course, if you
then tried to print them using %g, you'd still have a problem, because integers
are not floats (could use %n instead though).

I also used tconc to avoid reversing the list.

Regards,

Andrew.

Hi,

In my situation, the numbers are not necessarily be integers..it can
also be floating point numbers. Is there any method available to
convert string to floats?

Andrew,

What is "car(retList)" processing?

I am trying to use your code (....input data are integers only) in
following manner:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fout=outfile("/home/samiran/Desktop/output.txt")

let((pIn data retList)
        pIn = infile("/home/samiran/Desktop/input.txt")
        while(data=lineread(pIn)
                unless(data==t
                       retList=tconc(retList data)
                )
        )
        close(pIn)
        car(retList)

        foreach(list1 retList
                foreach(num list1
                        fprintf(fout "%n " num)
                )
                newline(fout)
        )
)

newline(fout)
close(fout)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The error is:

*Error* fprintf/sprintf: format spec. incompatible with data - (1 2 3
4)

BR
Samiran

How can I access a list element-by-element? I mean can I convert a
list to an array? Using foreach(...) gives me the element values to
the same variable, but I want like this - suppose if a list contains N
elements, then I want 1st element to be stored into 'var1', 2nd to be
stored into 'var2' and so on.

BR
Samiran
Andrew & Marcel,


Thanks for the help. I have figured out how to do.....your suggestions
really helped me a lot!


BR
Samiran
 
Samiran wrote, on 09/02/10 05:42:
What is "car(retList)" processing?
Because the list is a "tconc" structure, the actual list you're interested in is
car(retList) - look at the documentation for tconc.

If you're going to add printing in the same function, you would probably want to do:

retList=car(retList)

and then continue. The statement without the assignment worked because it was
the last statement in the let and hence was the return value.

Regards,

Andrew.
 
Samiran wrote, on 09/02/10 07:44:
How can I access a list element-by-element? I mean can I convert a
list to an array? Using foreach(...) gives me the element values to
the same variable, but I want like this - suppose if a list contains N
elements, then I want 1st element to be stored into 'var1', 2nd to be
stored into 'var2' and so on.


BR
Samiran
Do you really want to do that? The whole point of a list is that it has a
variable number of elements. Creating lots of variables such as this is a very
non-LISP or non-SKILL way of doing things, and would not ben terribly efficient.

Regards,

Andrew.
 
On Sep 1, 10:42 pm, Samiran <samiran....@gmail.com> wrote:
In my situation, the numbers are not necessarily be integers..it can
also be floating point numbers. Is there any method available to
convert string to floats?
The evalstring() function might be what you want. From what I can
tell, it doesn't automatically convert to float; it appears to detect
float or integer based on the presence of a decimal point. Watch how
it behaves in these command line examples:


ocean> a="2"
"2"
ocean> evalstring(a)
2
ocean> evalstring(a)/2
1
ocean> evalstring(a)/3
0

There, you can see that the result of evalstring(a) was treated as an
integer. But if the original string has a decimal point:

ocean> a="2.0"
"2.0"
ocean> evalstring(a)/3
0.6666667

then the result of evalstring(a) will be a float.

Best,
Stephen Greenwood
 
Stephen Greenwood wrote, on 09/13/10 22:05:
On Sep 1, 10:42 pm, Samiran<samiran....@gmail.com> wrote:
In my situation, the numbers are not necessarily be integers..it can
also be floating point numbers. Is there any method available to
convert string to floats?

The evalstring() function might be what you want. From what I can
tell, it doesn't automatically convert to float; it appears to detect
float or integer based on the presence of a decimal point. Watch how
it behaves in these command line examples:


ocean> a="2"
"2"
ocean> evalstring(a)
2
ocean> evalstring(a)/2
1
ocean> evalstring(a)/3
0

There, you can see that the result of evalstring(a) was treated as an
integer. But if the original string has a decimal point:

ocean> a="2.0"
"2.0"
ocean> evalstring(a)/3
0.6666667

then the result of evalstring(a) will be a float.

Best,
Stephen Greenwood
Stephen,

I never like using evalstring() for things like this - what if the string you
had accidentally contained the string "exit()" ?

Instead you could use atof() to convert the string to a float. Or if you want to
handle engineering suffixes (e.g. "6u") then you could use cdfParseFloatString().

Regards,

Andrew.
 
On Sep 15, 2:30 am, Andrew Beckett <andr...@DcEaLdEeTnEcTe.HcIoSm>
wrote:
Stephen,

I never like using evalstring() for things like this - what if the string you
had accidentally contained the string "exit()" ?

Instead you could use atof() to convert the string to a float. Or if you want to
handle engineering suffixes (e.g. "6u") then you could use cdfParseFloatString().

Regards,

Andrew.
Thanks, Andrew. I haven't had any problems with evalstring(), since
I've always had control over its argument, but you're right, atof()
and cdfParseFloatString() are more robust. Thanks for pointing them
out.

Best Regards,
Stephen
 

Welcome to EDABoard.com

Sponsor

Back
Top