Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple loop ( my memory is not good)

Status
Not open for further replies.

Bryan - Gendev

Programmer
Jan 9, 2011
408
AU
I can't think how to do this..
FOR i= 1 to 24
Strtofile((cEntry+STR(i)) +' ' +cPath+Chr(013)+Chr(10),('garble.log'),.T.)
readpwini(inifile,'write',cSection,(cEntry+STR(i)),cPath)

endfor

Thanks

Gendev

 
That is a tiny bit obtuse, what are you hoping to achieve?

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
Mike,

Sorry!
I have 24 lines like
Strtofile((cEntry1 +' ' +cPath+Chr(013)+Chr(10),('garble.log'),.T.)
readpwini(inifile,'write',cSection,(cEntry1,cPath)
to cover the variables cEntry1 to cEntry24

I need to concatenate the i value with cEntry to run them in the for next loop.

What I came up with gives me an error.

Is this clearer?

GenDev

 
So, you've got 24 separate variables, named cEntry1, cEntry2, and so on up to cEntry24?

And you want each line to contain the contents of each of those variables in turn?

If that's right, then instead of [tt]cEntry+STR(i))[/tt], you should use [tt]EVAL("cEntry" + TRANSFORM(i))[/tt].

At first glance, the rest of your syntax looks OK (although I can't speak for the call to Readpwin(), as I don't know anything about that function).

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
By the way, it will be more efficient to do something like this:

Code:
lcLine = ""
FOR lnI = 1 to 24
  lcI = TRANSFORM(lnI)
  lcLine = EVAL("cEntry) + lcI) + " " + cPath + CHR(13) + CHR(10))
ENDFOR
STRTOFILE(lcLine, "garble.log")

Again, I haven't addressed the call to Readpwin(). But it looks like you've got a syntax error in that line - possibly a spuious left parenthesis just before cEntry1.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
I assume those are fields. Well, look into AFIELDS() and even simpler FCOUNT() and FIELD(n).

Bye, Olaf.


 
Hi all,

Just to clarify
the 23 standard line pairs in my code are for cEntry1 to cEntry24
as an example cEntry1 might be a path statement -
cPath = ' '
1 - readpwini(inifile,'write',cSection,cEntry1,cPath)
2 - Strtofile(cEntry1 +' ' +cPath+Chr(013)+Chr(10),'garble.log'),.T.)

I am trying to do better code by putting these actions in a for next loop
- the first line writes a new value (cPath) to the ini file for the each entry cEntryn
- the second line writes to my log file which saves a record of the action.
As is the code works perfectly for the 24 variables.
I'm sorry I didn't say this before.
Now to the code offers
Code:
lcLine = ""
FOR lnI = 1 to 24
  lcI = TRANSFORM(lnI)
  lcLine = EVAL("cEntry) + lcI) + " " + cPath + CHR(13) + CHR(10))
ENDFOR
STRTOFILE(lcLine, "garble.log")

I believe the strtofile should be within the for next as I want a line in my log for each action.

so
Code:
lcLine = ""
FOR lnI = 1 to 24
  lcI = TRANSFORM(lnI)
  lcLine = EVAL("cEntry) + lcI) + " " + cPath + CHR(13) + CHR(10))
STRTOFILE(lcLine, "garble.log")
ENDFOR
this leaves the code to write to the ini file which also belongs in the for next loop.
readpwini(inifile,'write',cSection,cEntry1,cPath)

I need to get the content of each cEntry
Mike,
Unfortunately the line
lcLine = EVAL("cEntry) + lcI) + " " + cPath + CHR(13) + CHR(10))
compiles with an error
GenDev
 
Code:
* Simplify the above same code so you may be able to see the syntax error
lcCrlf = CHR(13) + CHR(10)
lcLine = ""
FOR lnI = 1 to 24
   lcI = TRANSFORM(lnI)
   lcLine = "CENTRY" + lcI
   lcLine = EVAL(lcLine)
   lcLine = lcLine + " "
   lcLine = lcLine + lcCrLf
   STRTOFILE(lcLine, "garble.log",1)  && To append to the file
ENDFOR
 
I believe the strtofile should be within the for next as I want a line in my log for each action.

No, that's not correct.

In my code, I am creating all 24 log entries in a single variable. There's nothing wrong with that. The fact that each entry is on a separate physical line makes no difference. Having created that variable, I am writing it to the file in a single action - outside the loop.

The effect is the same as in your original code, but my method is more efficient as it only required one disk access, not 24.

That said, it occures to me that you are not creating a new log file from scratch, but rather that you want to append these 24 entries to an existing log file. If that's correct, my code will still work, but you will need to pass .T. as the third parameter to STRTOFILE().

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Thanks Mike,

I couldn't run your code due to the error I mentioned in my previous post so I missed your building of the string.

I quote

'Unfortunately the line
lcLine = EVAL("cEntry) + lcI) + " " + cPath + CHR(13) + CHR(10))
compiles with an error.'

and I haven't been able to sort out this staement

readpwini(inifile,'write',cSection,cEntry1,cPath)

I need to get the actual contents of each string in here eg cEntry1. etc.

Thanks
GenDev
 
I just noticed that there is a double-quotes missing, immediately after [tt]"cEntry[/tt]. In other words, instead of this"

[tt]"cEntry[/tt]

you need this:

[tt]"cEntry"[/tt]

This was my fault, as it was an error my earlier post. But a glance at the code would have told you what was wrong. In general, it's always a good idea to try to figure out any incorrect syntax for yourself before you rush to post a message. It also helps if you could tell us what the error message says, rather than simply saying there is an error.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top