Sign in to follow this  
Followers 0
SKris

string copy

8 posts in this topic

i have text file with some coordinates like this:

123,434

453,975

827,832

...

i want to make a script to copy those coords in another file like this:

$A[1]="123"

$A[2]="453"

$A[3]="827"

.....

and

$B[1]="434"

$B[2]="975"

$B[3]="832"

...

Can u help me out? thankyou

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I start off by admitting that I am not an expert, but I would go trough these steps:

1) StringSplit all the strings in the first text file with "," as string delimiter

2) Assign the [1] value to array named $A[$i]

3) Assign the [2] value to array named $B[$i]

Example:

$File = FileOpen("file1.txt", 0)
If $File = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

While 1
    $Line = FileReadLine($File)
    If @error = -1 Then ExitLoop
    $array=StringSplit($line, ",")

;->>That would result in having an array like this for the first two values
;array[0] => 2
;array[1] => 123
;array[2] => 434

    $A[$i]=$array[1]    
    $B[$i]=$array[2]    
    $i=$i+1
WEnd

This sould work, for what you asked.

Notice that I haven't tested this, so double check the code I wrote, I might have wrote down something wrong ~

Cheers,

r3dbull.

Edited by r3dbull

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

You are talking about a normal CSV file, if you search the forum for that then you'll see that this is a pretty common problem.

If you think it through then it is simple:

  • Read the file (FileRead)
  • Split it into lines (StringSplit with @CRLF and option 1)
  • Size the arrays! Resizing is a slow process, and you know the length in advance so use it. (Redim)
  • Using the next line, split it into its ',' parts (StringSplit with ',')
  • Add the parts into the arrays (The array has already been sized in part 3 so its just a simple assignment.
  • Go back to part 4 until you've done all the lines
Where needed I've added function names and other info. It should be easy to do with the helpfile.

The only thing which won't make sense unless you've programmed for a while is why you need to use option 1 in stringSplit with @CRLF. The reason is that @CRLF = @CR & @LF, so is not one character. You StringSplit(string, Chr1 & Chr2) splits on Chr1 OR Chr2, so you will get blank entries in the array.

Mat

Edit: The above example is missing WEnd at the end of the loop, and the While $i<100 can just be While 1 as the loop will exit when there are no more lines to read (The IF test after FileReadLine does that for you).

Edited by Mat

Share this post


Link to post
Share on other sites

Your avatar looks familiar? You're not Hannes123 are you?


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_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: 04/09/2015

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Thanks Mat for your reply, I adjusted the above code for Skris if he would like to test it.

Edited by r3dbull

Share this post


Link to post
Share on other sites

If the paired data have a continuing relationship with one another, it might be more sensible to use a 2-d array so that

array[1][1] = 123

array[1][2] = 434

array[2][1] = 453

array[2][1] = 975 etc

just to keep them together and make it easier to use later in your script.

William

Share this post


Link to post
Share on other sites

thankyou so much guys

i used Malkey method and it works perfect. ty

Share this post


Link to post
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
Sign in to follow this  
Followers 0