Jump to content

Splitting array values into new columns with multiple delim's


Go to solution Solved by rickybobby,

Recommended Posts

I have a database of names, street names (no white space), and zipcodes captured via console output from our old "custom" catalog software; I've gotten the array cleaned up nicely but now i'm hitting a roadblock. The data has multiple delim's those being (,:,@CRLF), below is an example of a data set:
 
Row|Col 0
[0]|johnwindingroad:39501 (There are carriage returns after the end of each zip)
[1]|sallymadeupname:12345
[2]|rickimwaytootired:54321
.....
[N]|XY:Z
 
I want to split this data into columns of Names, Addresses, and Zip's.
 
I'm probably just too tired to get it, but any help is always appreciated!
Link to comment
Share on other sites

Hi. You could thi something like this:

#include <Array.au3>


Local $aArray=FileReadToArray("1.txt")
Local $aNewArray[UBound($aArray)][3]
local $aSplit=0
for $i= 0 to UBound($aArray)-1
    $aSplit=StringRegExp($aArray[$i],"[a-zA-Z0-9]+",3)
    $aNewArray[$i][0]=$aSplit[0]
    $aNewArray[$i][1]=$aSplit[1]
    $aNewArray[$i][2]=$aSplit[2]
Next

_ArrayDisplay($aNewArray)

Saludos

Edited by Danyfirex
Link to comment
Share on other sites

Hi. You could thi something like this:

#include <Array.au3>


Local $aArray=FileReadToArray("1.txt")
Local $aNewArray[UBound($aArray)][3]
local $aSplit=0
for $i= 0 to UBound($aArray)-1
    $aSplit=StringRegExp($aArray[$i],"[a-zA-Z0-9]+",3)
    $aNewArray[$i][0]=$aSplit[0]
    $aNewArray[$i][1]=$aSplit[1]
    $aNewArray[$i][2]=$aSplit[2]
Next

_ArrayDisplay($aNewArray)

Saludos

That only seems to work if the delim's were whitespace; again my delim's are () ( : ) and (@CRLF) in that order.

Edited by rickybobby
Link to comment
Share on other sites

Just use the function StringSplit and use all 3 of your characters as delimiters and use flag 0 ($STR_CHRSPLIT) so that it splits on each character.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Just use the function StringSplit and use all 3 of your characters as delimiters and use flag 0 ($STR_CHRSPLIT) so that it splits on each character.

Just tried that it splits it into new rows, I wanted the array to have new columns; ex:

 ORIGINAL:

Row|Col 0
[0]|johnwindingroad:39501 (There are carriage returns after the end of each zip)
[1]|sallymadeupname:12345
[2]|rickimwaytootired:54321
 
NEW ARRAY:
Row|Col 0     |Col 1       |Col 2
[0]|   john  |windingroad| 39501 (There are carriage returns after the end of each zip)
[1]|  sally  |madeupname|12345
[2]|  rick   |imwaytootired|54321
 
Hope that makes it more clear what I'm trying to get at!
Link to comment
Share on other sites

Here's how I did it.

#include <Array.au3>
Global $sFile = "file.txt"
Global $sText = FileRead($sFile)
Global $aText = StringSplit($sText, ":\" & @CR)
_ArrayDisplay($aText)
Global $aNewArray[$aText[0] / 3][3]
$Count = 0
For $I = 1 To $aText[0] - 1 Step 3 ; you only need the -1 if the last line is blank due to the last line feed
    $aNewArray[$Count][0] = $aText[$I]
    $aNewArray[$Count][1] = $aText[$I + 1]
    $aNewArray[$Count][2] = $aText[$I + 2]
    $Count += 1
Next
_ArrayDisplay($aNewArray)

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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...