Sign in to follow this  
Followers 0
evod510

String Chunk

14 posts in this topic

#1 ·  Posted (edited)

Greetings,

First of all thank you for making this program B)

I need help :whistle: , i dont know whether autoit can do it or not

I have a string of text(combination of 88 byte ) which look like this

10371512711635  +00011619000+00000000000+00000000000+00000000000+00000000000+000000000

each byte/count have its own meaning, example the 1st byte/count lets say 5 is the product key , the 2nd byte/count 10 is the product code

I need to separete the code to look something like this

10371;512711635  +;00011619000+;00000000000+;etc

the reason is to import the files into excel , and it will be in its own column

after that i need to convert the product key to its description example

10371 is apple etc

Hope you understand and I hope somebody can help.

Thanks

Edited by evod510

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

So You want to put a semicolon after each plus sign and also after the first five characters. Here's one way:

; Assume $keyCode is your string
$tmp = StringReplace($keyCode, "+", "+;")
$tmp = StringLeft($tmp,5) & ";" & StringTrimLeft($tmp, 5)

P.S. You'll get quicker responses by posting questions to the support forum :whistle:

Edited by CyberSlug

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post


Link to post
Share on other sites

actually I did post the same question at

http://forums.devshed.com/t110240/s.html

i was trying to use phyton. It was bit difficult,

and now i am trying to use autoit to solve the problem

help please

Share this post


Link to post
Share on other sites

what about something like this ?

$var = "10371512711635  +00011619000+00000000000+00000000000+00000000000+00000000000+000000000"
$out = StringMid($var,1,5) & ";" & StringReplace(StringTrimleft($var,6),'+','+;')

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Greetings,

First of all thanks for you help, but I think you misunderstand my question (i didnt explain correctly)

I start from the beginning. i need to convert a file to a coma deliminated format ( so i can open that file using excel)

I have a text file generated by another program.

Lets say this is the content of the text file.

abcd1234efgh5678i9j0k1l2mabcd1234efgh5678i9j0k1l2mabcd1234efgh5678i9j0k1l2m

Each 25 strings contain code of one transaction

so abcd1234efgh5678i9j0k1l2m contain "abcd"(string 1-4) as code product, "1234" (string 5-8) as code vendor,

"efgh"(string 9-12) as code date, "5678" (string 13-16) as code location and "i9j0k1l2m"(string 17-25) as code value.

Inside the file contain more than 1 transaction each transaction will use the

same string format as above to record other transaction.

ok i hope you understand

so this is code that i wrote

;While 1

$file = FileOpen("test.txt", 0); open file

$line = FileReadline($file)      ; readline
$code = StringMid($line, 1,4)  ; read string
$vendor = StringMid($line, 5,4)
$date = StringMid($line, 9,4)
$location = StringMid($line, 13,4)
$value = StringMid($line, 17,9)

$file2 = FileOpen("write.txt", 1); open file to write

FileWrite($file2, $line & ";")   
FileWrite($file2, $code & ";")
FileWrite($file2, $vendor & ";")
FileWrite($file2, $date & ";")
FileWrite($file2, $location & ";")
FileWrite($file2, $value & @CRLF)  ; each transaction will be on a newline

FileClose($file)

;Wend

I only manage to produce the first transaction, I did put the while function but it will loop forever and it will

only take the first 25 string (first transaction).

I got confuse with the while function.

Please guide me.

Thanks in advance :whistle:)

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

your script changed so it will read all records...

; open the file and test if ok
$FILE = FileOpen("test.txt", 0); open file
If $FILE = -1 Then
   MsgBox(0, "Error", "Unable to open file.")
   Exit
EndIf
$FILE2 = FileOpen("write.txt", 1); open file to write

; endless loop
While 1
  ; Read line and if @error then EOF so exit loop
   $LINE = FileReadLine($FILE)    ; readline
   If @error = -1 Then ExitLoop
   $CODE = StringMid($LINE, 1,4); read string
   $VENDOR = StringMid($LINE, 5,4)
   $DATE = StringMid($LINE, 9,4)
   $LOCATION = StringMid($LINE, 13,4)
   $VALUE = StringMid($LINE, 17,9)
  ; write records
   FileWrite($FILE2, $LINE & ";")   
   FileWrite($FILE2, $CODE & ";")
   FileWrite($FILE2, $VENDOR & ";")
   FileWrite($FILE2, $DATE & ";")
   FileWrite($FILE2, $LOCATION & ";")
   FileWrite($FILE2, $VALUE & @CRLF); each transaction will be on a newline
Wend
FileClose($FILE)
FileClose($FILE2)
Edited by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Thanks ( :whistle: )

But it still show only the first transaction

Share this post


Link to post
Share on other sites

Here are the working code B)

; ----------------------------------------------------------------------------
; Count String
; ----------------------------------------------------------------------------
$FILE = FileOpen("test.txt", 0); open file
If $FILE = -1 Then
  MsgBox(0, "Error", "Unable to open file.")
  Exit
EndIf
While 1
    $chars = FileRead($file, 1)
    If @error = -1 Then ExitLoop
 $i=$i+1
Wend
; ----------------------------------------------------------------------------
; Count Transaction In String
; ----------------------------------------------------------------------------
$trans = $i/25
FileClose($FILE)
; ----------------------------------------------------------------------------
; Open File
; ----------------------------------------------------------------------------
$FILE = FileOpen("test.txt", 0); open file
If $FILE = -1 Then
  MsgBox(0, "Error", "Unable to open file.")
  Exit
EndIf
$FILE2 = FileOpen("write.txt", 1); open file to write
$LINE = FileReadLine($FILE)
; ----------------------------------------------------------------------------
; Set Variable
; ----------------------------------------------------------------------------
Dim $y = 0
$ii = 0
$iii = 1
; ----------------------------------------------------------------------------
; Loop
; ----------------------------------------------------------------------------
Do
Dim $xcode = $y+1
Dim $xVENDOR = $y+5
Dim $xDATE = $y+9
Dim $xLOCATION = $y+13
Dim $xVALUE = $y+17
; ----------------
;Read String
; ----------------
  $CODE = StringMid($LINE, $xcode,4); read string
  $VENDOR = StringMid($LINE, $xVENDOR,4)
  $DATE = StringMid($LINE, $xDATE,4)
  $LOCATION = StringMid($LINE, $xLOCATION,4)
  $VALUE = StringMid($LINE, $xVALUE,9)
; ----------------
;Write String
; ----------------
  FileWrite($FILE2, $CODE & ";")
  FileWrite($FILE2, $VENDOR & ";")
  FileWrite($FILE2, $DATE & ";")
  FileWrite($FILE2, $LOCATION & ";")
  FileWrite($FILE2, $VALUE & @CRLF)
; ----------------
;Adjust Variable
; ----------------
Dim $y=25*$iii 
$ii = $ii + 1
$iii = $ii + 1
Until $ii = $trans
; ----------------------------------------------------------------------------
; End
; ----------------------------------------------------------------------------
MsgBox(0, "Transaction Recorded", $trans)
FileClose($FILE)
FileClose($FILE2)

Anyway to shorten it :whistle:

Share this post


Link to post
Share on other sites

Looks to me that your input file doesn't contain regular End_Of_records (CRLF) so the whole thing is considered 1 record.

Thats also probably why you get one output record.

You are now reading all characters in one at the time and then spilt the record up on sections of 25 characters...

So what characters is at pos 25 ? can that be used to split the record up into an array ?


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

anything can be on string 25

actually the code above work ok

maybe a bit long

now i need to convert the code into its name

if $CODE = abcd then
              $var1 = "Apple"
            endif

if i got 10 code i have to put in 10 name.

so i will have 10 if code. is there any otherway

iis it possible to use array.

Please Help

:whistle:

Edited by evod510

Share this post


Link to post
Share on other sites

Sounds to me the easiest way is to use a IF on one line:

if $CODE = abcd then $var1 = "Apple"

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

thanks ....

need a bit more help

what if the code entered is not recognise

how do i make a condition for that??

Thanks

Share this post


Link to post
Share on other sites

Then you could do a Select case like:

Select
   Case $CODE = "abcd"
        $var1 = "Apple"
   Case $CODE = "xyz" 
        $var1 = "banana"
   Case Else
        $var1 = "No Fruit"
EndSelect

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

JdeB,

Thanks a lot. :whistle:

Thanks again

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