Jump to content
Sign in to follow this  
RyukShini

Splitting string/file

Recommended Posts

RyukShini

I have a string looking like this:
Outsider:myemail@myemail.com:26e0112200304f6d7598f6bd90a8478d:_KPQX)B7C0IX~!QgqG*V*){X<71O{{

This is how my database looks, I would like to split my file in to
User
Email
Hash:Salt

The reason why I wish to do this is to have an easier view of my users, I may also want to encrypt passwords even further and I cannot do this with a string looking like that!

Thanks in advance.

Share this post


Link to post
Share on other sites
RTFC
#include <Array.au3>

$string="Outsider:myemail@myemail.com:26e0112200304f6d7598f6bd90a8478d:_KPQX)B7C0IX~!QgqG*V*){X<71O{{"
$split=StringSplit($string,":")
_ArrayDisplay($split)

NB: If colons can appear in your salt, you would have to catenate $split entries 4-N if split[0]>4.

Edited by RTFC

Share this post


Link to post
Share on other sites
RyukShini
8 minutes ago, RTFC said:
#include <Array.au3>

$string="Outsider:myemail@myemail.com:26e0112200304f6d7598f6bd90a8478d:_KPQX)B7C0IX~!QgqG*V*){X<71O{{"
$split=StringSplit($string,":")
_ArrayDisplay($split)

NB: If colons can appear in your salt, you would have to catenate $split entries 4-N if split[0]>4.

Hi thanks, it works!
but what if a string contains this:
tester:test@test.com:9b6af4443774867abc06358645aa4cdb:D>!+\,6gR!sJ+{:q5Z]Wy2/!q:0PXZ

As you can see their are : within the salt.
Thanks in advance!

Share this post


Link to post
Share on other sites
RTFC

Please note my comment below the code snippet in my previous post. In short:

1. check if $split[0]>4; if not, you're done

2. add  colon plus next part of salt to start of salt for all parts >4

#include <Array.au3>

$string="tester:test@test.com:9b6af4443774867abc06358645aa4cdb:D>!+\,6gR!sJ+{:q5Z]Wy2/!q:0PXZ"
$split=stringsplit($string,":")
For $sc=5 to $split[0]
    $split[4]&=":"&$split[$sc]
Next
Redim $split[5]
_ArrayDisplay($split)

So the For...Next loop is carried out only if $split[0] is at least 5.

Alternatively you could store the salt as a hexstring, and convert to binary where necessary.

Edited by RTFC

Share this post


Link to post
Share on other sites
RyukShini
7 minutes ago, RTFC said:

Please note my comment below the code snippet. In short:

1. check if $split[0]>4, if not, you're done

2. add  colon plus next part of salt to start of salt for all parts >4

#include <Array.au3>

$string="tester:test@test.com:9b6af4443774867abc06358645aa4cdb:D>!+\,6gR!sJ+{:q5Z]Wy2/!q:0PXZ"
$split=stringsplit($string,":")
For $sc=5 to $split[0]
    $split[4]&=":"&$split[$sc]
Next
Redim $split[5]
_ArrayDisplay($split)

So the For...Next loop is carried out only if $split[0] is at least 5.

Works great.
Now if  I have a file with say 500 lines, I could write 500 instead of 5, but couldn't I load the line number in a variable if I am unsure how many lines their is in the file I wish to organize?

Thanks in advance.

Share this post


Link to post
Share on other sites
RTFC

Please read up on FileReadLine and _FileWriteFromArray in the Help file. Read an entry, split it, store it.

You can of course also store the 1D array $split into a new row of your own 2D array in each read-iteration.

That should be sufficient to get you started, methinks.

Share this post


Link to post
Share on other sites
RyukShini
Just now, RTFC said:

Please read up on FileReadLine and _FileWriteFromArray in the Help file. Read an entry, split it, store it.

You can of course also store the 1D array $split into a new row of your own 2D array in each read-iteration.

That should be sufficient to get you started, methinks.

#include <Array.au3>

$file = FileRead("new.txt")
$split=StringSplit($file,":")
For $sc=5 to $split[0]
    $split[5]&=":"&$split[$sc]
Next
Redim $split[5]
_ArrayDisplay($split)

It works fine, but this still gives me 5 only.
I want it to read all of the file, so maybe I should use [$i] or something?
maybe.

For $i = 1 to $line or something similar?
Thanks in advance.

Share this post


Link to post
Share on other sites
RyukShini
20 minutes ago, RTFC said:

Check FileReadLine, not FileRead.

I made it work, but now it will only load 4 lines.

028bfeada69e7ea1cc1f1bc0657e2d0c.png

Now the first 4 lines works fine, but the rest is blank??
Thanks in advance
 

#include <Array.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
$file = "new.txt"
Local $hFileOpen = FileOpen("new.txt", $FO_READ)

Local $sFileRead = FileReadLine($hFileOpen, 1)
$split=StringSplit($sFileRead,":")
For $sc=100 to $split[0]
    $split[100]&=":"&$split[$sc]
Next
Redim $split[100]
_ArrayDisplay($split)

 

Share this post


Link to post
Share on other sites
RTFC

Okay, one last try.:x

As I told you before ("in each read iteration"), you need to repeat the stringsplit for every line of your file, and store the data per line. Something like this (haven't tested it).

#include <Array.au3>
#include <File.au3>
#include <FileConstants.au3>

$file = "new.txt"
$lines=_FileCountLines($file)
global $list[$lines+1][4]
Local $hFileOpen = FileOpen($file, $FO_READ)

Local $curline=0
Local $sFileRead = FileReadLine($hFileOpen, 1)
while not @error
    $split=StringSplit($sFileRead,":")
    For $sc=5 to $split[0]
        $split[4]&=":"&$split[$sc]
    Next

    $curline+=1
    for $sc=1 to 4
        $list[$curline][$sc-1]=$split[$sc]
    next
    $sFileRead = FileReadLine($hFileOpen)
WEnd
FileClose($hFileOpen)
$list[0]=$lines
_ArrayDisplay($list)

 

Edited by RTFC
bug

Share this post


Link to post
Share on other sites
RyukShini
6 minutes ago, RTFC said:

Okay, one last try.:x

As I told you before ("in each read iteration"), you need to repeat the stringsplit for every line of your file, and store the data per line. Something like this (haven't tested it).

#include <Array.au3>
#include <File.au3>
#include <FileConstants.au3>

$file = "new.txt"
$lines=_FileCountLines($file)
global $list[$lines+1][4]
Local $hFileOpen = FileOpen($file, $FO_READ)

Local $curline=0
Local $sFileRead = FileReadLine($hFileOpen, 1)
while not @error
    $split=StringSplit($sFileRead,":")
    For $sc=5 to $split[0]
        $split[4]&=":"&$split[$sc]
    Next

    $curline+=1
    for $sc=1 to 4
        $list[$curline][$sc-1]=$split[$sc]
    next
    $sFileRead = FileReadLine($hFileOpen)
WEnd
FileClose($hFileOpen)
$list[0]=$lines
_ArrayDisplay($list)

 

Sorry, will figure it out!
Thanks a lot for the time and effort you put in this.

Share this post


Link to post
Share on other sites
RTFC

You're welcome, RyukShini.:)

I'm sure you'll get the hang of it. If not, I would suggest you study the Help example of FileReadLine and read up on 2D arrays.

Good luck,

RT

Share this post


Link to post
Share on other sites
rudi

Hi.

I'd like to suggest, to read the full file to an array using _filereadtoarray(). Then loop through this array to process your lines. This will give you a start, I think:

#include <file.au3>
#include <array.au3>


dim $aLines
dim $file="C:\temp\your-input.txt"

$f=FileOpen($file,2+8)

FileWriteLine($f,"Outsider:myemail@myemail.com:26e0112200304f6d7598f6bd90a8478d:_KPQX)B7C0IX~!QgqG*V*){X<71O{{")
FileWriteLine($f,"Insider:InEmail@myemail.com:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:111111111111111111111111111111")
FileWriteLine($f,"upsider:UpEmail@myemail.com:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb:222222222222222222222222222222")
FileClose($f)


_FileReadToArray($File,$aLines)

if IsArray($aLines) Then
    _ArrayDisplay($aLines)
Else
    MsgBox(0,"","not an array")
    Exit
EndIf


for $i = 1 to $aLines[0]
    if StringStripWS($aLines[$i],8)="" Then
        MsgBox(0,"Empty line (or blanks only)","Line #" & $i)
        ContinueLoop
    EndIf
    $aFoo=StringSplit($aLines[$i],":")
    if IsArray($aFoo) Then
        _ArrayDisplay($aFoo)
    Else
        MsgBox(0,"Line " & $i,"The result isn't an array!" & @CRLF & _
        "String:" & @CRLF & _
        $aLines[$i])
    EndIf
Next

regards, Rudi.

 

<edit: Can the salt have ":" in its string?>

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites
AutoBert

i also sugest reading the full fill:

#include <Array.au3>
#include <File.au3>

Global $aAccounts, $iFilledRows

_FileReadToArray('accounts.txt', $aAccounts, $FRTA_NOCOUNT)
;, ':');using splitting in Cols fails on files with diferent colcounts in line
If Not @error Then
    _ArrayColInsert($aAccounts, 1)
    _ArrayColInsert($aAccounts, 1)
    _ArrayColInsert($aAccounts, 1)
    For $iRows = 0 To UBound($aAccounts) - 1
        If Not StringIsSpace($aAccounts[$iRows][0]) Then
            $aSplit = StringSplit($aAccounts[$iRows][0], ':', 2)
            For $iCols = 0 To 2
                $aAccounts[$iRows][$iCols] = $aSplit[$iCols]
            Next
            $sSalt = ''
            For $iCols = 3 To UBound($aSplit) - 1
                $sSalt &= $aSplit[$iCols]
            Next
            $aAccounts[$iRows][3] = $sSalt
            $iFilledRows += 1
        EndIf
    Next
    ReDim $aAccounts[$iFilledRows][4]
EndIf
_ArrayDisplay($aAccounts)

 

Share this post


Link to post
Share on other sites
RyukShini
19 hours ago, AutoBert said:

i also sugest reading the full fill:

#include <Array.au3>
#include <File.au3>

Global $aAccounts, $iFilledRows

_FileReadToArray('accounts.txt', $aAccounts, $FRTA_NOCOUNT)
;, ':');using splitting in Cols fails on files with diferent colcounts in line
If Not @error Then
    _ArrayColInsert($aAccounts, 1)
    _ArrayColInsert($aAccounts, 1)
    _ArrayColInsert($aAccounts, 1)
    For $iRows = 0 To UBound($aAccounts) - 1
        If Not StringIsSpace($aAccounts[$iRows][0]) Then
            $aSplit = StringSplit($aAccounts[$iRows][0], ':', 2)
            For $iCols = 0 To 2
                $aAccounts[$iRows][$iCols] = $aSplit[$iCols]
            Next
            $sSalt = ''
            For $iCols = 3 To UBound($aSplit) - 1
                $sSalt &= $aSplit[$iCols]
            Next
            $aAccounts[$iRows][3] = $sSalt
            $iFilledRows += 1
        EndIf
    Next
    ReDim $aAccounts[$iFilledRows][4]
EndIf
_ArrayDisplay($aAccounts)

 

Thanks, it works as intended, but I don't know why, I just can't seem to understand how to get it to display all of the file....
right now it still only displayed 39 results :S
Thanks in advance.

Share this post


Link to post
Share on other sites
RyukShini
20 hours ago, rudi said:

Hi.

I'd like to suggest, to read the full file to an array using _filereadtoarray(). Then loop through this array to process your lines. This will give you a start, I think:

#include <file.au3>
#include <array.au3>


dim $aLines
dim $file="C:\temp\your-input.txt"

$f=FileOpen($file,2+8)

FileWriteLine($f,"Outsider:myemail@myemail.com:26e0112200304f6d7598f6bd90a8478d:_KPQX)B7C0IX~!QgqG*V*){X<71O{{")
FileWriteLine($f,"Insider:InEmail@myemail.com:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:111111111111111111111111111111")
FileWriteLine($f,"upsider:UpEmail@myemail.com:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb:222222222222222222222222222222")
FileClose($f)


_FileReadToArray($File,$aLines)

if IsArray($aLines) Then
    _ArrayDisplay($aLines)
Else
    MsgBox(0,"","not an array")
    Exit
EndIf


for $i = 1 to $aLines[0]
    if StringStripWS($aLines[$i],8)="" Then
        MsgBox(0,"Empty line (or blanks only)","Line #" & $i)
        ContinueLoop
    EndIf
    $aFoo=StringSplit($aLines[$i],":")
    if IsArray($aFoo) Then
        _ArrayDisplay($aFoo)
    Else
        MsgBox(0,"Line " & $i,"The result isn't an array!" & @CRLF & _
        "String:" & @CRLF & _
        $aLines[$i])
    EndIf
Next

regards, Rudi.

 

<edit: Can the salt have ":" in its string?>

Yes it can have ":" in the salt :/

Share this post


Link to post
Share on other sites
RyukShini
22 hours ago, RTFC said:

You're welcome, RyukShini.:)

I'm sure you'll get the hang of it. If not, I would suggest you study the Help example of FileReadLine and read up on 2D arrays.

Good luck,

RT

Thanks, doing this now.
Its strange cos i feel i've done more complicated stuff in the past, but this one is just giving me a headache :/

Share this post


Link to post
Share on other sites
mikell

You could do it using a regex, it can be more accurate in case of tricky splits
(error checking missing in the code below)

#include <Array.au3>

$str = "this is a test:indeed" & @crlf & _
    "Outsider:myemail@myemail.com:26e0112200304f6d7598f6bd90a8478d:_KPQX)B7C0IX~!QgqG*V*){X<71O{{" & _ 
    @crlf & _
    "Insider:InEmail@myemail.com:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1111111111111:1111111:111111:1111" & _
    @crlf & _ 
    "tester:test@test.com:9b6af4443774867abc06358645aa4cdb:D>!+\,6gR!sJ+{:q5Z]Wy2/!q:0PXZ"

$tmp = StringRegExp($str, '(?m)^(.+?):(.+?):(.*)\R?', 3)
Local $res[UBound($tmp)/3][3]
   For $i = 0 to UBound($tmp)-1 step 3
       For $j = 0 to 2
          $res[$i/3][$j] = $tmp[$i+$j]
       Next
   Next
_ArrayDisplay($res)

 

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  

  • Similar Content

    • VADemon
      By VADemon
      I've encountered a problem with a single file where I cannot retrieve it's Date-time. So far my code has worked well for over 30 files, but this one is a mystery I cannot debug myself due to insufficient Au3 knowledge.
      In line 11 "_Date_Time_FileTimeToArray" is called and for this particular file it sets the @error to 10. I don't know what that error code means, but it's not set by the _Date functions themselves I think.
      Overall, it could be a problem caused by any of the functions below, how can I properly debug this? / Does anybody know a what's causing this?
      _WinAPI_CreateFile() / _Date_Time_GetFileTime() / _Date_Time_FileTimeToArray()
      Func _SetFileTimes($sFilePath) Local $monthNumber[13] = ["", "January", "February", "March", "April", "May", "Juny", "July", "August", "September", "October", "November", "December"] Local $dayNumber[7] = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] Local $fHandle = _WinAPI_CreateFile($sFilePath, 2, 2) ; read-only ; may NOT return a valid date for some reason! TODO Local $fTagFILETIME = _Date_Time_GetFileTime($fHandle) _WinAPI_CloseHandle($fHandle) ; This will return an empty array if theres no valid date $fModTime = _Date_Time_FileTimeToArray($fTagFILETIME[2]) ; last Modified if @error <> 10 then Local $year = $fModTime[2] Local $month = $fModTime[0] Local $day = $fModTime[1] Local $hour = $fModTime[3] Local $min = $fModTime[4] Local $sec = $fModTime[5] Local $ms = $fModTime[6] Local $weekday = $fModTime[7] Global $prettyTimestamp = StringFormat("%s, %s %d, %04d %02d:%02d:%02d", $dayNumber[$weekday], $monthNumber[$month], $day, $year, $hour, $min, $sec) Global $uploadDate = StringFormat("%04d-%02d-%02d", $year, $month, $day) $fModTime = _Date_Time_FileTimeToArray(_Date_Time_FileTimeToLocalFileTime($fTagFILETIME[2])) ; last Modified Local $year = $fModTime[2] Local $month = $fModTime[0] Local $day = $fModTime[1] Local $hour = $fModTime[3] Local $min = $fModTime[4] Local $sec = $fModTime[5] Local $ms = $fModTime[6] Local $weekday = $fModTime[7] ; GetUnixTime accounts for Local time, hence feed it local time Global $unixTimestamp = _GetUnixTime($year &"/"& $month &"/"& $day &" "& $hour&":"& $min &":"& $sec) else Global $prettyTimestamp = "N/A" Global $uploadDate = "" Global $unixTimestamp = "N/A" endif endfunc  
      _GetUnixTime returned the year 1601 start date, showing that $fModTime is probably equal 0. (But Why?)
      The file reports these dates in Explorer, it's on local NTFS drive:
      Created: ‎‎Wednesday, ‎31. ‎Januar ‎2018, ‏‎18:55:02
      Modified: ‎Wednesday, ‎10. ‎Januar ‎2018, ‏‎12:39:23
      Accessed: ‎Wednesday, ‎10. ‎Januar ‎2018, ‏‎12:39:23
    • nooneclose
      By nooneclose
      I need help turning this string "20180913221626" into a formatted time string.
      I need to go from this: 20180913221626
      to this: 09/13/2018 10:16 PM
      I do not always know what the date will be so I can not just use a variable I need to actually convert/format. 
      I did see an older post in the forms that was basically the same question only the other guy did not post the working code and I can not figure out how to use _AD_GetObjectProperties properly to get what I want. 
      As always any help would be appreciated. 
      Here is the code I use to find the date, but it always gives me the unformatted version. 
      ;retrieve the items object $oItem = $oOutlook.Session.GetItemFromID($aItems[1][0], Default) $oItem.GetInspector $eSentOn = $oItem.SentOn ; When was the email sent? MsgBox("", "Sent On of the email", $eSentOn) ;******************************************************************************* ; Formats the date and time from the email ;******************************************************************************* ;Local $fDatenTime = _DateTimeFormat($eSentOn, 1) ;MsgBox("", "Formatted email time", $fDatenTime) $aProperties = _AD_GetObjectProperties($eSentOn) _ArrayDisplay($aProperties, "Did the conversion work?")  
    • Miliardsto
      By Miliardsto
      I got that func
      Func makeHelpImgGUI($title,$width,$height,$img) $img = GUICtrlCreatePic("",20,40,$width,$height) _ResourceSetImageToCtrl($img, "HERE") EndFunc and I call this func like that
      makeHelpImgGUI("Image",1190, 800,$SETTINGS_JPG)  
      so what is the problem in the parameter where is - "HERE" I need value of img but passed as string
      so $img = $SETTINGS_JPG and how make it "SETTINGS_JPG"
       
      I tried something like that but not work
      Func makeHelpImgGUI($title,$width,$height,$img) $name_str = String($img) $name_str = StringTrimLeft ($name_str, 1 ) $img = GUICtrlCreatePic("",20,40,$width,$height) _ResourceSetImageToCtrl($img, $name_str) EndFunc  
    • liagason
      By liagason
      Hello everyone,
      How can I display in ascending  sequence some numbers stored in a string variable?
      $str = "18,03,48,23" MsgBox(0,"test",$str) I would like it to display "03,18,23,48"
    • Rskm
      By Rskm
      Hi, I have the following line in a text file 'input.txt'. I know the line number - say '6'. I wish to replace the text 'WWW' in the below line with a random number (I can generate that with random()).
      WERIS  WWWJP   3.83  8.330  1.000                1097.RAXX 
      The WWW is a 3 digit integer (could be any number between 0 to 999), I can use stringtrimleft and get the numerical value of WWW in this file
      so, basically, I know the string to replace (ie; WWW stored in a variable), I know the line number to work on and the file location/name and the replacement variable (through random()). My requirement is to fill that 3 spaces with my random number (which Is a integer between 1 and 999)
      please put ur suggestions
       
×