Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'StreamPosition in function' topicStudent Support Forum > General > Archives > "StreamPosition in function"

< Previous CommentHelp | Reply To Comment | Reply To Topic
Author Comment/Response
Forum Moderator
email me
10/20/00 12:21pm

>Below is an example of StreamPosition not working when called in a function.
>
>First comes the file myfile.txt containing the company name ''my name''; we want to retrieve a number which happens to be 23.75.
>
>
>(begin of myfile.txt)
>
> <td bgcolor=''#ecf4fd'' align=''left''><font size=''1'' face=''Arial'' color=''#000094''><NOBR>my name</NOBR></font></td>
>         <td bgcolor=''#ffffff'' align=''center''><font face=Arial size=''1''>23.75</font></td>
>         <td bgcolor=''#FF6363'' align=''center''><font face=Arial size=''1''><img
>         
>(end of myfile.txt)
>
>I would expect the following code to take us to the right place. The Find command succeeds in taking us to the company name, but StreamPosition returns 0.
>
>
>findRate[companyName_, fileName_] :=
> Module[{pos,sMyfile}, sMyfile := OpenRead[fileName];
>SetStreamPosition[sMyfile, 0];
>Find[sMyfile, companyName];
>Skip[sMyfile, Word, 4];
>pos:=StreamPosition[sMyfile];
>pos+=10;
>SetStreamposition[sMyfile,pos]]
>
>The last seven commands by themselves work all right. But
>
>findRate[''my name'', ''c:\\myfile.txt'']
>
>just trivially returns 10. Did I make any newcomer's mistake?


=========

You can try to get some info from your function while it runs by putting in temporary Print commands:


In[88]:=
findRate[companyName_, fileName_] :=
Module[{pos,sMyfile}, sMyfile := OpenRead[fileName];

SetStreamPosition[sMyfile, 0];
Find[sMyfile, companyName];
Print[StreamPosition[sMyfile]];
       Skip[sMyfile, Word, 4];
         Print[StreamPosition[sMyfile]];
pos:=StreamPosition[sMyfile];
            Print[StreamPosition[sMyfile]];
pos+=10;
SetStreamposition[sMyfile,pos];

   Print[pos];
   Print[StreamPosition[sMyfile]]
      Close[sMyfile];]

In[89]:= findRate[''my name'', ''Hard Drive:Desktop Folder:test.txt'']
0
0
0
10
0

The issue is your use of Set Delayed ( := ), i.e some things that you want to evaluate are not. If you use Set ( = ) things should look better, e.g.

In[89]:=
findRate[companyName_, fileName_] :=
Module[{pos,sMyfile}, sMyfile = OpenRead[fileName];

SetStreamPosition[sMyfile, 0];
Find[sMyfile, companyName];
Print[StreamPosition[sMyfile]];
       Skip[sMyfile, Word, 4];
         Print[StreamPosition[sMyfile]];
pos=StreamPosition[sMyfile];
            Print[StreamPosition[sMyfile]];
pos+=10;
SetStreamposition[sMyfile,pos];

   Print[pos];
   Print[StreamPosition[sMyfile]]
      Close[sMyfile];]
In[90]:=
findRate[''my name'', ''Hard Drive:Desktop Folder:test.txt'']
116
174
174
184
174

The difference between pos and the StreamPosition in the last two values is due to the uncapitalized ''p'' in SetStreamposition[sMyfile,pos];


Tom Zeller
Forum Moderator


URL: ,

Subject (listing for 'StreamPosition in function')
Author Date Posted
StreamPosition in function Carol 10/17/00 06:44am
Re: StreamPosition in function Forum Modera... 10/20/00 12:21pm
< Previous CommentHelp | Reply To Comment | Reply To Topic