Sign in to follow this  
Followers 0
williamk

Array Problem

9 posts in this topic

Need to write a script that will take a txt file with several thousand lines. Each line looking something like:

AME401|AMEZCUA |JOSE |CERVANTES | 9.00|630004|CR|

Need to rewrite the lines so that they look like this:

AME401;AMEZCUA, JOSE CERVANTES; 9.00;630004;CR

So am basically removing a few delimeters, taking out the white space and replacing | with ;. I think the best way to do this to write each line to an array and then just write the array out to a new file in the format I want. However, the following code gives me the error "Array variable has incorrect number of subscripts or subscript dimension range exceeded.: "

What am I doing wrong?

#include <file.au3>
#include <array.au3>
DIM $extract
$dirpath = "C:\" 
$outdir = "C:\"
$empaddfn = "mast256.txt"
$outf = Fileopen ($outdir & "empadd.txt",1)
If Not _FileReadToArray($dirpath & $empaddfn,$extract) Then 
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf
        For $x = 1 to $extract[0] 
        $array = StringSplit($extract[$x], "|") ; split the current line into $array using "|" as delimeter
        FileWrite($outf, $array[1] & ";" & $array[2] & ", " & $array[3] & " " & $array[4] & ";" & $array[5] & ";" & $array[6] & ";" & $array[7] & @CRLF)
        Next
FileClose ($outf)

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

For $x = 1 to $extract[0] -1 <--- this should fix

Would have written it a bit different:

$inputfile = Fileopen("mast256.txt",0)

$outputfile = Fileopen("empadd.txt",2)

while 1

$line = FileReadLine($inputfile)

if $line = "" then exitloop

$array = StringSplit($line, "|") ; split the current line into $array using "|" as delimeter

FileWrite($outf, $array[1] & ";" & $array[2] & ", " & $array[3] & " " & $array[4] & ";" & $array[5] & ";" & $array[6] & ";" & $array[7] & @CRLF)

Wend

FileClose ($inputfile )

FileClose($outputfile)

Edited by Kademlia

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Why not do it this way?

For $x = 1 to $extract[0]
  $extract[$x] = StringReplace($extract[$x], "|",";",1)  ; the first occurrence
  $extract[$x] = StringReplace($extract[$x], "|",",",1)  ; the second one
  $extract[$x] = StringReplace($extract[$x], "|",";")   ; all the rest
  FileWrite($outf,$extract[$x] & @CRLF)
Next
Edited by torun

Share this post


Link to post
Share on other sites

For $x = 1 to $extract[0] -1 <--- this should fix

No it shouldn't. This would make it skip the last line.

"be smart, drink your wine"

Share this post


Link to post
Share on other sites

Why not do it this way?

For $x = 1 to $extract[0]
  $extract[$x] = StringReplace($extract[$x], "|",";",1) ; the first occurrence
  $extract[$x] = StringReplace($extract[$x], "|",",",1) ; the second one
  $extract[$x] = StringReplace($extract[$x], "|",";"); all the rest
  FileWrite($outf,$extract[$x] & @CRLF)
Next
Hey guys,

Yep either solution works. Cool. One last thing. How can I strip the white space (spaces) out of each element and or line?

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Check the last record (line) of the input file. If it is empty, remove it (remove the last CRLF).

to remove white spaces:

For $x = 1 to $extract[0]
  $extract[$x] = StringReplace($extract[$x], " |","|"); spaces before '|'
  $extract[$x] = StringReplace($extract[$x], "| ","|"); spaces following '|'

  $extract[$x] = StringReplace($extract[$x], "|",";",1); the first occurrence
  $extract[$x] = StringReplace($extract[$x], "|",",",1); the second one
  $extract[$x] = StringReplace($extract[$x], "|",";"); all the rest

  FileWrite($outf,$extract[$x] & @CRLF)
Next
Edited by torun

Share this post


Link to post
Share on other sites

Check the last record (line) of the input file. If it is empty, remove it (remove the last CRLF).

to remove white spaces:

For $x = 1 to $extract[0]
  $extract[$x] = StringReplace($extract[$x], " |","|"); spaces before '|'
  $extract[$x] = StringReplace($extract[$x], "| ","|"); spaces following '|'

  $extract[$x] = StringReplace($extract[$x], "|",";",1); the first occurrence
  $extract[$x] = StringReplace($extract[$x], "|",",",1); the second one
  $extract[$x] = StringReplace($extract[$x], "|",";"); all the rest

  FileWrite($outf,$extract[$x] & @CRLF)
Next
That didn't seem to work. This however did:

#include <file.au3>
#include <array.au3>
DIM $extract
$dirpath = "C:\" 
$outdir = "C:\"
$empaddfn = "mast256.txt"
$inf = $dirpath & $empaddfn
$outf = Fileopen ($outdir & "empadd.txt",2)
If Not _FileReadToArray($inf,$extract) Then 
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf
        For $x = 1 to $extract[0] -1
        $array = StringSplit($extract[$x], "|") ; split the current line into $array using "|" as delimeter
        $array[2] = stringstripWS ($array[2],2)
        $array[3] = stringstripWS ($array[3],2)
        $array[4] = stringstripWS ($array[4],3)
        $array[5] = stringstripWS ($array[5],1)
            if $array[7] = "MT" Then
            FileWrite($outf, $array[1] & ";" & $array[2] & ", " & $array[3] & " " & $array[4] & ";" & $array[5] & ";" & $array[6] & ";" & $array[7] & @CRLF)
            Endif
        Next
FileClose ($outf)
FileClose ($inf)

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

This works here too:

$inputfile = Fileopen("in.txt",0)
$outputfile = Fileopen("out.txt",2)

while 1
$line = FileReadLine($inputfile)
if $line = "" then exitloop
    $line = StringReplace($line," |","|")  ; spaces before '|'
    $line = StringReplace($line,"| ","|")  ; spaces following '|'

    $line = StringReplace($line, "|",";",1); the first occurrence
    $line = StringReplace($line, "|",",",1); the second one
    $line = StringReplace($line, "|",";")  ; all the rest
    FileWrite($outputfile,$line & @CRLF)
Wend

FileClose ($inputfile )
FileClose($outputfile)
Edited by torun

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Or even shorter:

$inputfile = Fileopen("in.txt",0)
$outputfile = Fileopen("out.txt",2)

while 1
$line = FileReadLine($inputfile)
if $line = "" then exitloop
    $line = StringRegExpReplace($line,"(\|.+?) {0,1}\| {0,1}","$1,",1) 
    $line = StringRegExpReplace($line," {0,1}\| {0,1}",";")
    FileWrite($outputfile,$line & @CRLF)
Wend

FileClose ($inputfile )
FileClose($outputfile)
Edited by torun

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