MariusN Posted March 4, 2010 Share Posted March 4, 2010 Hope someone can help me here...ive been battling the whole day, and dunno if i'm just plain stupid...over-consentration or WHAT, but i just cant get this right... I need to right a Variable to the 3rd line of a file. The variable is "unknown", so using "_FileWriteToLine" is out of the question...Any help? PS: there are 5 lines all together. Link to comment Share on other sites More sharing options...
golfinhu Posted March 4, 2010 Share Posted March 4, 2010 Hope someone can help me here...ive been battling the whole day, and dunno if i'm just plain stupid...over-consentration or WHAT, but i just cant get this right...I need to right a Variable to the 3rd line of a file. The variable is "unknown", so using "_FileWriteToLine" is out of the question...Any help? PS: there are 5 lines all together.I do not quite understand what you want, you want to get the value of the third line of a file is it?you can use the _filereadtoarray Link to comment Share on other sites More sharing options...
Fulano Posted March 4, 2010 Share Posted March 4, 2010 Please give us some example code to work with: debugging normally is hard enough, please don't blindfold us #fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja! Link to comment Share on other sites More sharing options...
MariusN Posted March 4, 2010 Author Share Posted March 4, 2010 I am busy compiling a Bandwidth monitor and it requires quite some calculations etc. I have a log file with 5 lines in it...now my prob is not READING from the 3rd line, but actually "replacing" the 3rd line with an unknown value using netstat-e...ie In a normal scenario, this would work---> 1) _FileWriteToLine(@ScriptDir & "\settings.ini", 3, "20", 1)...meaning "20" will be inserted in line 3... Now seeing i DONT know the value, the "20" must now be replaced with an UNKNOWN value... ie) FileWriteToLine(@ScriptDir & "\settings.ini", 3, $var1, 1) Now if you look above, you see i have changed the "20" with $var(the unknown value)...Unfortunatly the above function doesnt work with Variables and it MUST be a value... What i'm looking for, is there any type of function you guys know of that can be used to place an UNKNOWN value into any given line-no, for example line 3? (hope i make some sense as this is the best way to explain this) Link to comment Share on other sites More sharing options...
Fulano Posted March 4, 2010 Share Posted March 4, 2010 (edited) For the love of Pete, you've only got 5 lines! 1: Read the file in using FileOpen, FileRead 2: Split into an array using StringSplit on the CRLF 3: Edit the line you want in the array 4: Overwrite the file contents with the lines of the array Edited March 4, 2010 by Fulano #fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja! Link to comment Share on other sites More sharing options...
MariusN Posted March 4, 2010 Author Share Posted March 4, 2010 I was trying to keep the code as simple as possible...thought there might be a short-cut...Will use that then...thx anyways Link to comment Share on other sites More sharing options...
Fulano Posted March 4, 2010 Share Posted March 4, 2010 (edited) That is, more likely than not, essentially what a shortcut function would do, if you want your code to be clean, just turn it into a function. Like as not it will be more efficient than loading a multi-function UDF into your script.Also: simple doesn't always mean shorter. Take a look at these two 'FizzBuzz' implementations in perl, and I'd bet money that the simpler one is not the shorter:Perl FizzBuzz (Short Version)Perl FizzBuzz (Long Version) Edited March 4, 2010 by Fulano #fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja! Link to comment Share on other sites More sharing options...
jchd Posted March 4, 2010 Share Posted March 4, 2010 _FileReadToArray $arr[2] = $newvalue _FileWriteFromArray Hardly complex. See details in the Holy Help File. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
MariusN Posted March 4, 2010 Author Share Posted March 4, 2010 (edited) here is a snippet of my code...the $var doesnt come from a file, it comes from a calculation and the diference is the $var...No problem to add this in a 3rd file and work from there, but i like to have limited files lying around...if you know what i mean? Func _Replace() $rnd3 = $rnd1 + $rnd2 $open2 = FileOpen($fileLog, 0); ROOT --READ $read1 = FileReadLine($open2, 1) FileClose($open2) $open6 = FileOpen($fileLog, 2) FileWrite($open6, $rnd3);ROOT--WRITE FileClose($open6) $open3 = FileOpen($fileLog2, 0);SYSTEM--READ $read3 = FileReadLine($open3, 1) FileClose($open3) Sleep(20) $open4 = FileOpen($fileLog, 0);ROOT--READ $read2 = FileReadLine($open4, 2) FileClose($open4) $total = $read3 - $read2 _FileWriteToLine(@SystemDir & "\netbackup.txt",3,$total,1) EndFunc ;==>_Replace I can always put the $total in another file as thats a piece of pie, but my mistake was that i thought FileWriteToLine could handle $var's (as shown in the helpfile) to my supprise (after about 12 hours) i realized FileWriteToLine couldnt do it...lol Edited March 4, 2010 by MariusN Link to comment Share on other sites More sharing options...
Fulano Posted March 4, 2010 Share Posted March 4, 2010 (edited) <rant>Please, please, please give your variables names which mean something!</rant>I can't tell from your code, but there are probably problems with Local vs Global variables, as the numbering seems to indicate the existence of other variables.Also, what are the files you are reading from supposed to look like, are they just integers on a line, etc?What data is coming into this?I've done what I could to clean it up, but it is still confusing as to what you want to code to do.;Assuming Global Variables: $gFileLog, $gFileLog2, $rnd1, $rnd2 Func Replace($targetFile) Local $writeLine = $rnd1 + $rnd2 #Region The variables in the section are never refered to again, why does this code exist? Local $tempHandle = FileOpen($gFileLog, 0) Local $fileContents1 = FileReadLine($tempHandle, 1) FileClose($tempHandle) #EndRegion $tempHandle = FileOpen($gFileLog, 2) FileWrite($tempHandle, $writeLine) FileClose($tempHandle) $tempHandle = FileOpen($gFileLog2, 0) Local $fileContents2 = FileReadLine($tempHandle, 1) FileClose($tempHandle) Sleep(20) ; Why is this here? That is only 20 miliseconds. $tempHandle = FileOpen($gFileLog, 0) Local $fileContents3 = FileReadLine($tempHandle, 2) FileClose($tempHandle) Local $total = $fileContents1 - $fileContents3 _FileWriteToLine($targetFile, 3, $total, 1) EndFunc Edited March 4, 2010 by Fulano #fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja! Link to comment Share on other sites More sharing options...
MariusN Posted March 4, 2010 Author Share Posted March 4, 2010 _FileReadToArray $arr[2] = $newvalue _FileWriteFromArray Hardly complex. See details in the Holy Help File. looks like this will work...thx jchd Link to comment Share on other sites More sharing options...
MariusN Posted March 4, 2010 Author Share Posted March 4, 2010 <rant>Please, please, please give your variables names which mean something!</rant> I can't tell from your code, but there are probably problems with Local vs Global variables, as the numbering seems to indicate the existence of other variables. Also, what are the files you are reading from supposed to look like, are they just integers on a line, etc? What data is coming into this? I've done what I could to clean it up, but it is still confusing as to what you want to code to do. ;Assuming Global Variables: $gFileLog, $gFileLog2, $rnd1, $rnd2 Func Replace($targetFile) Local $writeLine = $rnd1 + $rnd2 #Region The variables in the section are never refered to again, why does this code exist? Local $tempHandle = FileOpen($gFileLog, 0) Local $fileContents1 = FileReadLine($tempHandle, 1) FileClose($tempHandle) #EndRegion $tempHandle = FileOpen($gFileLog, 2) FileWrite($tempHandle, $writeLine) FileClose($tempHandle) $tempHandle = FileOpen($gFileLog2, 0) Local $fileContents2 = FileReadLine($tempHandle, 1) FileClose($tempHandle) Sleep(20) ; Why is this here? That is only 20 miliseconds. $tempHandle = FileOpen($gFileLog, 0) Local $fileContents3 = FileReadLine($tempHandle, 2) FileClose($tempHandle) Local $total = $fileContents1 - $fileContents3 _FileWriteToLine($targetFile, 3, $total, 1) EndFunc thanks for helping but seeing youre so full of yourself i'll rather use jchd's help....thx Link to comment Share on other sites More sharing options...
Fulano Posted March 4, 2010 Share Posted March 4, 2010 (edited) ::shrugs:: Good luck with your script, anyway Edited March 4, 2010 by Fulano #fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja! Link to comment Share on other sites More sharing options...
jchd Posted March 4, 2010 Share Posted March 4, 2010 @MariusN, Please don't dismiss Fulano's advices that fast. They are all good practice that, if you force yourself to stick to them, will make your programming life _way_ easier and avoid piles of frustration and wasted time. I don't know Fulano, probably not more than you do, but he clearly has gone thru more or less painful learning and is kind enough to expose some guidelines to help you learn faster and with less visits to dead-ends. So don't believe he's "full" of h(im|er)self, just trying to push you towards the steepest part of the learning curve. And it's about the same with most regular contributors here. The best reward is when people show they are making progress by their own, even if they sometimes get stuck with a more delicate notion, concept or just overlooking something. Better enjoy a friendly, lively and helpful forum! This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
AdmiralAlkex Posted March 4, 2010 Share Posted March 4, 2010 ie) FileWriteToLine(@ScriptDir & "\settings.ini", 3, $var1, 1)Now if you look above, you see i have changed the "20" with $var(the unknown value)...Unfortunatly the abovefunction doesnt work with Variables and it MUST be a value...What are you talking about?$var1 = "20"FileWriteToLine(@ScriptDir & "\settings.ini", 3, $var1, 1)And_FileWriteToLine(@ScriptDir & "\settings.ini", 3, "20", 1)Is the EXACT same thing! If that really doesn't work in your script, then show us a short and working (runnable) reproducer so someone can help you. We are not mind-readers. .Some of my scripts: ShiftER, Codec-Control, Resolution switcher for HTC ShiftSome of my UDFs: SDL UDF, SetDefaultDllDirectories, Converting GDI+ Bitmap/Image to SDL Surface Link to comment Share on other sites More sharing options...
MariusN Posted March 5, 2010 Author Share Posted March 5, 2010 (edited) What are you talking about?$var1 = "20"FileWriteToLine(@ScriptDir & "\settings.ini", 3, $var1, 1)And_FileWriteToLine(@ScriptDir & "\settings.ini", 3, "20", 1)Is the EXACT same thing! If that really doesn't work in your script, then show us a short and working (runnable) reproducer so someone can help you. We are not mind-readers.Ooo f f's sakes....how can i explain this better...lolololol (Sorry Admiral, the laugh is not intended on you, i just whish i was ENGLISH then maybe it would have come through better...but let me rephrase...)Ok, here goes --->I am busy compiling a UTILITY (Bandwidth Monitor) where i get the information via the "netstat -e" command. I have a TimerInit() that will "grab" the current status of the "bytes" that goes through my NIC (network adapter). The VALUE is UNKNOWN to me as it gets GRABED (excuse my english) every , lets say 20 secs. Now, this "Value" i want to add into a text file, but it must be put in line "3" of my text file. All i was asking is HOW to get the "value" in the 3RD line....thats all...Not how my program work etc etc as its working 99% without the VALUE i want to put in line "3"....now here is my prob...1) FileWriteToLine(@ScriptDir & "\settings.ini", 3, $var1, 1)====Does NOT work because you can NOT place a $var1 in a FileWriteToLine, it has to be a "fixed" number ie "20" or "30" or whatever the case may be. I need only a small piece of information...I want to know WHAT function i can use that can use a $var and put it in line "3" for me.....that's all!(remember, i DONT know the value of &Var1)Basicly i need a command that can REPLACE --->FileWriteToLine(@ScriptDir & "\settings.ini", 3, $var1, 1)<---...it must be able to accept a Variable.--->FulanoI appologise if i came across rude...In my mind it was a simple question and not HOW my program works...i just needed a replacement to the _FileWriteToLine command...thats all. All the other "Sleeps" you were talking about was just things i were experementing with...I'm using TimerInit() by the way...My script is still VERY "raw" and there is still days ahead before its finished HERE IS MY PREVIOUS CONVERSATION WITH m23 My link Edited March 5, 2010 by MariusN Link to comment Share on other sites More sharing options...
enaiman Posted March 5, 2010 Share Posted March 5, 2010 Read the whole file in a variable, replace the 3rd line, clear the file content, write the file again using your variable, close the file. You can't simply "write to a line" - you can see that the UDF functions are doing the same thing (reding the content, replacing, writing to a temp file, rename the temp file with the original name) so this is the way you have to follow. Or: use an "ini file approach"; make your text file ini-like. SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script wannabe "Unbeatable" Tic-Tac-Toe Paper-Scissor-Rock ... try to beat it anyway :) Link to comment Share on other sites More sharing options...
MariusN Posted March 5, 2010 Author Share Posted March 5, 2010 (edited) Am i glad YOU understood me, lol...and whalla...that's what i wanted to know...Thanks a bunch enaiman To all the other guys that tried to help? I DO appreciate...It's just that i was so frustrated with the FileWriteToLine because it took me nearly a whole day just to find out it cant use variables...Also when i program, i first start of with a "structure" (irrespectively of how bad the code looks)...Once finished, then i fix the code Edited March 5, 2010 by MariusN Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted March 5, 2010 Moderators Share Posted March 5, 2010 MariusN, It's just that i was so frustrated with the FileWriteToLine because it took me nearly a whole day just to find out it cant use variablesPlease do not spread inaccurate information. _FileWriteToLine works perfectly well with variables both as the file name and the text, as I showed you here. You might not be able to use it, for reasons you never fully determined, but that does not mean others cannot. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
czardas Posted March 5, 2010 Share Posted March 5, 2010 AdmiralAlkex and Melba23 are both right. If the variable is a number, perhaps converting it from a number to a string will fix the problem. I could be wrong, but it might be worth a try? operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now