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

    • Trisha
      By Trisha
      Hello,
      I Have clicked on save as option to save a file, while doing that I need to rename a file appending with sysdate. I have searched in google find the below one line of code:
      FileMove("C:\somefile.txt", "C:\somefile1.txt"), When I am trying to append with sysdate. It is not happening. Please help me out  with the small issue.
    • WoodGrain
      By WoodGrain
      Hi All,
      I'd like to replace 'COMMA' with ',' for example:
      $myString = "COMMA" StringRegExpReplace($myString, 'COMMA', ',') Now I've tried escaping the ',' in various ways unsuccessfully, such as:
      '[,]'
      "[,]"
      '\,'
      [,] seems to work in the pattern, I just can't figure out how to use it in the replace, and it seems everyone online is only interested in removing/replacing commas lol.
      I also tried creating and using a variable as the replacement but also didn't work:
      $myComma = "," $myString = "COMMA" StringRegExpReplace($myString, 'COMMA', $myComma) I'm sure it's super simple if someone could point me in the right direction - thanks.
    • kartune
      By kartune
      Hello, I am getting this error message when running my script:
      Case $aButiD[0] To $aButiD[$iTotButtons - 1]
      Case ^ ERROR
      Error:  Array variable has incorrect number of subscripts or subscript dimension range exceeded.
       
      The script's purpose is to read my ini file and create buttons for the sections and section contents.
      The first part of the script creates buttons for the sections of the ini, once you click on the section button it should open another set of buttons of all the contents under the section
      In this case, the section in the .ini file is called "TPA"
      on the Func flavorco()
      It opens the contents of "TPA" into buttons.
      Right now, the section it is reading is determined by $sSection.
      It is only when I try to change $sSection = "TPA"
      to
      $sSection = $flavorco
      that the error starts to appear
       
      Here is the script
      #include <MsgBoxConstants.au3> #include <WinAPIFiles.au3> #include <Array.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <ButtonConstants.au3> $optbar = IniReadSectionNames("test.ini") If Not IsArray($optbar) Then Exit Local $Button[UBound($optbar) - 1] $mGUI = GUICreate('Restock Flavor Ingredients', 10 + (130 * UBound($optbar)), 100) For $x = 1 To UBound($optbar) - 1 $var = IniReadSection("test.ini", $optbar[$x]) If @error Then ContinueLoop $X_Coord = 10 + (($x - 1) * 100) $Button[$x - 1] = GUICtrlCreateButton($optbar[$x], $X_Coord, 30, 70, 30) Next GUISetState() While 1 $mMsg = GUIGetMsg() If $mMsg = $GUI_EVENT_CLOSE Then GUIDelete($mGUI) ExitLoop EndIf For $i = 0 To UBound($Button) - 1 If $mMsg = $Button[$i] Then MsgBox(0, '$Msg = ' & $mMsg, GuiCtrlRead($Button[$i]) & ' Pressed', 2) Global $flavorco = $Button ; sets the flavor company's variable Call('flavorco') EndIf Next WEnd Func flavorco() ;opened flavor company's flavors MsgBox(0, '$Msg = ' & $mMsg, GuiCtrlRead($Button[$i]) & $flavorco, 0) Local $sFilePath = @ScriptDir & "\Test.ini" Local $sSection = $Button Local $aArray = IniReadSection($sFilePath, $sSection) _ArrayDelete($aArray, 0) ;_ArrayDisplay($aArray) Local $iIndex Local $iTotButtons = UBound($aArray) Local $iNumPerRow = 5 Local $iButWidth = 100 Local $iButDepth = 30 Local $aButiD[$iTotButtons] Local $hGUI = GUICreate('Ingredients Stock "' & $sFilePath & '"', 10 + ($iButWidth + 10) * $iNumPerRow, _ 10 + Ceiling($iTotButtons / $iNumPerRow) * ($iButDepth + 10), -1, $WS_EX_TOPMOST) For $i = 0 To $iTotButtons - 1 ; $x = $iXBorder + (($iRectWidth + $iSpacing) * Mod($i, $iNumCols)) $x = 10 + (($iButWidth + 10) * Mod($i, $iNumPerRow)) ; $y = $iYBorder + (($iRectDepth + $iSpacing) * Int($i / $iNumCols)) $y = 10 + (($iButDepth + 10) * Int($i / $iNumPerRow)) $aButiD[$i] = GUICtrlCreateButton($aArray[$i][0], $x, $y, $iButWidth, $iButDepth) ;, $BS_NOTIFY) Next GUISetState(@SW_SHOW) While 1 $msg = GUIGetMsg() Switch $msg Case $GUI_EVENT_CLOSE GUIDelete($hGUI) ExitLoop Case $aButiD[0] To $aButiD[$iTotButtons - 1] ;clicking on a flavor name $iIndex = ($msg - $aButiD[0]) $RSINGRflavor = $aArray[$iIndex][0] ;flavor name $RSINGRml = $aArray[$iIndex][1] ; ml $RSINGRgal = $aArray[$iIndex][1]*0.000264172 ;converts ml to gal Call('openflavor') EndSwitch WEnd EndFunc Func openflavor() #Region ### START Koda GUI section ### Form=F:\Karl\AutoIt\FJ-Stock JP\GUI Forms\RSINGRSUBFORM.kxf ;opens flavor to view stock and restock submit $RSINGRSUBFORM = GUICreate($sSection & " " & $RSINGRflavor, 242, 213, 530, 269) $RSINGRFlavorTitle = GUICtrlCreateLabel($sSection & " " & $RSINGRflavor, 32, 8, 200, 28) GUICtrlSetFont(-1, 16, 800, 0, "MS Sans Serif") $RSINGRstockcaption = GUICtrlCreateLabel("Current Stock in ml:", 16, 48, 96, 17) $RSINGRstockml = GUICtrlCreateLabel($RSINGRml, 120, 48, 114, 25) GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif") $RSINGRInput = GUICtrlCreateInput("ml", 40, 112, 153, 21) $RSINGRSUBMIT = GUICtrlCreateButton("SUBMIT", 16, 152, 81, 33) $RSINGRCLOSE = GUICtrlCreateButton("CLOSE", 145, 151, 81, 33) $RSINGRgalcaption = GUICtrlCreateLabel("Current Stock in gal:", 16, 80, 97, 17) $RSINGRstockgal = GUICtrlCreateLabel($RSINGRGAL, 120, 80, 114, 25) GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif") GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### ; $msg = "" While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE GUIDelete($RSINGRSUBFORM) ExitLoop Case $RSINGRCLOSE GUIDelete($RSINGRSUBFORM) ExitLoop EndSwitch WEnd EndFunc  
      Any help is much appreciated!  Thanks.
      My guess is that $Button isn't an actual string??  Whenever I use msgbox it shows up the string exactly, no numbers attached.  So i'm not sure whats going on here.
    • XinYoung
      By XinYoung
      Hello again,
      I'm trying to use the FileMove function to rename a bunch of text files.
      Some of the files have "_1" at the end of their name, for example, "File123_1.txt". I want to remove the "_1" so it would become "File123.txt".
      What I currently have...
              FileMove($sSource & "\*_1.txt", $sDestination & "\*.txt")
      But since I'm using a wildcard, it doesn't appear to be working. I think it's just replacing .txt with .txt. Am I going about this the wrong way? How can I use wildcards and still accomplish this?
      Any help is greatly appreciated ^__^
    • ur
      By ur
      I have date in the string format as "DD-MM-YYYY".
       
      I need to get yesterday's date from it.
      I tried converting this from _DateTimeFormat but not working.
      Is there any direct UDF available to get this.?
×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.