Jump to content

FileWriteFromArray doesn't represent changes that are applied to the array


Recommended Posts

I know this looks similar to some other posts. I looked at a few and 1 of them I thought for sure was the answer but it didn't work so in desperation I am starting this thread.

So, I have a large text file that I am looking to replace the words "RUN DATE" when certain criteria isn't met. I through some MsgBox feedback etc in here so I could better understand what is happening. It seems the logic is working. It finds the instances where the criteria is met and makes (appears to) changes to the array which is evident when it performs the "MsgBox(0, "String Replace State", $StringReplace)" function i.e. I see the change to that array in the result. The problem is that when I do a _FileWriteFromArray and then look at the new file that is created it doesn't reflect the changes made. I know I am very close but just can't seem to figure this one out. I did try changing the $i_Base to 0 specifically as was mentioned on another thread (Thought for sure that would work) but still the same problem.

Here is my ugly code...please don't laugh

#include <Array.au3>
#include <File.au3>
$FileOpen = FileOpen ( "c:\trantest.txt")
Local $TranText = FileRead("c:\trantest.txt")
Local $TranArray = StringSplit($TranText, ' ', 1);This splits the .txt file into an array. Each array is the beginning of a new page which is what the 'FF' represents.
MsgBox(0, "Number of Strings in Array", $TranArray[0])

For $i = $TranArray[0] To 1 Step -1;This checks how many arrays the page has been split into so that we can loop through each array to test for the condition we are looking for.
$UnsignedTrans = 99999 ;We need to set an initial value here. If there is a page with RUN DATE that doesn't have the text 'Unsigned transcriptions" this value will be set to zero
$Search = StringInStr ( $TranArray[$i], "RUN DATE")
MsgBox(0, "Array Test", $Search)
If $Search <> 0 Then
  MsgBox(0, "String Found", "String Found in Array" & $i)
  Global $UnsignedTransTest = StringInStr($TranArray[$i], "Unsigned transcriptions")
    MsgBox(0, "Unsigned text exists?", $UnsignedTransTest)
    $UnsignedTrans = $UnsignedTransTest
    MsgBox(0, "2", $UnsignedTrans)
     EndIf
If $UnsignedTrans = 0 Then
MsgBox(0, "Error at line", $i)
Local $StringReplace = StringReplace($TranArray[$i], "RUN DATE", "xxxxxxx", 1)
_ArrayDisplay($TranArray)
$UnsignedTrans = 99999; this resets the variable so the next loop itteration can run correctly
;FileClose($FileOpen)
$FileWrite = _FileWriteFromArray("C:\test.txt", $TranArray)
MsgBox(0, "File Write State", $FileWrite)
MsgBox(0, "File Write Error", @error)
MsgBox(0, "String Replace State", $StringReplace)
  EndIf
Next
MsgBox(0, "", "Blast Off!")
Edited by itctravel
Link to comment
Share on other sites

Seems you confused yourself a lot there. Maybe because you name everything "tran"? :P

Some things to start with:

_ArrayDisplay($TranArray[$i])

There is no array in $TranArray[$i]. Maybe you meant

_ArrayDisplay($TranArray)

$FileWrite = _FileWriteFromArray("C:\test.txt", $TranText)

Again, $TranText is not an array, it's just the return from FileRead(). Not sure what you're trying to do there.

You also have FileClose() crazy far from where it would do any good, and you aren't actually using the filehandle from FileOpen() in FileRead(). If that's your whole script then FIleOpen() doesn't do any good anyway so I would just remove it.

And please encase your code with AutoIt tags in the future. Makes it much easier to read. Just put [autoit ] [ /autoit] around your code (without the spaces).

Edited by AdmiralAlkex
Link to comment
Share on other sites

Admiral;

Thanks for your reply.

I changed so much playing around that I accidently initially posted the wrong iteration of the program. Thanks for the tip about the autoit tags...I modified appropriately.

So to clarify what I am trying to do is I have 'divided' this long .txt into an array which is created with each 'new page' of the report

Local $TranArray = StringSplit($TranText, ' ', 1)

for some reason the special character I am splitting with doesn't translate over to this post but it is here and I verified that part is working.

Then I run through each array to see if the words "RUN DATE" exists without the words "Unsigned transcriptions" on the page. If it does, I want to replace the words "RUN DATE" with "xxxxxxx"

Sorry for prematurely posting the wrong code but what you see now is the best I have come to so far and seems to work except for the saving to file part. It does save the file but the file doesn't show the "xxxxxxx" changes I made in the loop.

I am wondering if there are special ASCII characters in my .txt file that could be preventing this from working? I know it is a long shot but I have racked my brain as to why the canges never 'take'

BTW, I like your boat :)

Edited by itctravel
Link to comment
Share on other sites

So I am trying to break this down to basics and I've done a simple experiment that shows the problem. I Created a .txt (test.txt) document that contains only the following line

"this, is, a, line, of, text"

I then perform this script

#include <Array.au3>
Local $TranText = FileRead("c:\test.txt")
Local $TranArray = StringSplit($TranText, ',')
Local $text = StringReplace($TranArray[1], "i", "-")
Local $numreplacements = @extended
MsgBox(0, "The number of replacements done was", $numreplacements)
_ArrayDisplay($TranArray)

The $numreplacements indicates a change was made but when you do the _ArrayDisplay, it doesn't reflect the change

For what it is worth, I am at version Version 3.2.0 for SciTE

Link to comment
Share on other sites

  • Developers

Makes sense when you are not updating the array with the result but putting it in $text. ;)

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Notice the difference between the two:

#include <Array.au3>

Local $sText = '"this, is, a, line, of, text"'; FileRead('Example.txt')
Local $aArray = StringSplit($sText, ',')
$aArray[1] = StringReplace($aArray[1], 'i', '-') ; Re-assign the replacement back to $aArray[1].
Local $iReplacements = @extended
MsgBox(4096, '', 'The number of replacements done was: ' & $iReplacements)
_ArrayDisplay($aArray)

So it's not AutoIt that is broken in this instance.

Edited by guinness

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

  • Moderators

itctravel,

Of course it will not change the array element - you never tell it to! :D

But this script does:

#include <Array.au3>
Local $TranText = FileRead("test.txt")
Local $TranArray = StringSplit($TranText, ',')
_ArrayDisplay($TranArray)
$TranArray[1] = StringReplace($TranArray[1], "i", "-")
Local $numreplacements = @extended
MsgBox(0, "The number of replacements done was", $numreplacements)
_ArrayDisplay($TranArray)

Can you see the difference. ;)

M23

Edit: Great minds eh! :D

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Edit: Great minds eh! :D

Aimed at me I presume. -_0 Edited by guinness

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

  • Moderators

guinness,

All 3 of us actually! :D

Although Jos cheated and did not provide an example script. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Also he meant $text not $test, but I think itctravel would have understood.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

  • Developers

Although Jos cheated and did not provide an example script. ;)

teach ...fish ... etc etc ;)

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

Thank you Sooooo much, especially Guinness and Melba. It's all about the details! I'm glad I gave ya'll a chuckle...just learning this stuff so I REALLY appreciate this support mechanism for a great program. Keep up the great work guys(gals)!!

mmm not sure i like this answer where you indicate to prefer to be spoon fed.... ;)

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Jos. You are reading too much into my interpretation. I enjoy learning. I think anyone messing with this stuff does. If yours were the only response it may have eventually lead me to the fix but it really wasn't stearing me on a course to efficient resolution as much as the others.

Link to comment
Share on other sites

  • Developers

Jos. You are reading too much into my interpretation. I enjoy learning. I think anyone messing with this stuff does. If yours were the only response it may have eventually lead me to the fix but it really wasn't stearing me on a course to efficient resolution as much as the others.

... and you are reading too much into my reply ... was just "playing" with you'all .... :)

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...