Jump to content

IniReadSection, strange behavior


Recommended Posts

I'm filling my ini file with two kinds of codes:

-----

CODE
For $i=1 To 10

$PhoNum2c=GUICtrlRead($PhoNumInp2c[$i])

$PhoNam2c=GUICtrlRead($PhoNamInp2c[$i])

If StringLen($PhoNum2c)=9 Then

If StringRegExp($PhoNum2c & "nothing", $IamPat, 0)=1 Then

IniWrite($NewLstFile, "IAM", $PhoNum2c, '"' & $PhoNam2c & '"')

ElseIf StringRegExp($PhoNum2c & "nothing", $MedPat, 0)=1 Then

IniWrite($NewLstFile, "MEDITEL", $PhoNum2c, '"' & $PhoNam2c & '"')

EndIf

EndIf

Next

------

CODE
For $i=1 To UBound($iamnum)

$t1="0" & $iamnum[$i-1]

$t2='"IAM Contact ' & $i & '"'

IniWrite($NewLstFile, "IAM", $t1, $t2)

Next

For $i=1 To UBound($mednum)

$t1="0" & $mednum[$i-1]

$t2='"MEDITEL Contact ' & $i & '"'

IniWrite($NewLstFile, "Meditel", $t1, $t2)

Next

------

both codes works fine, I have checked the ini file and found the expected results. But when I try to read all the keys created by the second code using IniReadSection function it reads only the values and ignores the keys. I have tried many tricks but with no success.

The weird thing is that both files (1st filled wwith the 1st code and 2nd file filled with the 2nd code) are visually identical.

I have attached an example of an ini file filled with the second code. You may execute the following code to see that it doesn't return the keys but only the values:

CODE
$var = IniReadSection("C:\TEST2.ini", "IAM")

If @error Then

MsgBox(4096, "", "Error occurred, probably no INI file.")

Else

For $i = 1 To $var[0][0]

MsgBox(4096, "", "Key: " & $var[$i][0] & @CRLF & "Value: " & $var[$i][1])

Next

EndIf

Thank you in advance for your help.

TEST2.zip

Link to comment
Share on other sites

  • Moderators

ElFarah,

Your ini files might look identical, but they are almost certainly not.

If you look at the ini you posted with a hex editor, you have a "0x0d" at the end of each keyname (the telephone number) as you can see from this hex dump.

000040  3d 30 32 3a 30 30 0d 0a 5b 49 41 4d 5d 0d 0a 30 36 38 33 34 31 32 33 39 0d 3d 22 49 41 4d 20 43  =02:00..[IAM]..068341239.="IAM C
000060  6f 6e 74 61 63 74 20 31 22 0d 0a 30 36 36 30 34 36 34 38 33 0d 3d 22 49 41 4d 20 43 6f 6e 74 61  ontact 1"..066046483.="IAM Conta

This is fooling IniSectionRead. Remove the "0x0d" and all is well!

So your second code snippet is somehow adding this extra byte to the number - while your first snippet does not, as you say it works perfectly. Without the rest of your code I cannot say how the extra byte gets there, but it looks as if the array $iamnum elements already hold it.

Hope this helps. Happy to probe further if you post enough code to let me do so.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thank you very much [Melba23], now I figured out the source of my problem.

Here are the patterns:

CODE
Global $MedPat='(1[2479][0-9]{6}\D|4[459][0-9]{6}\D)|56[0-9]{6}\D|6[03459][0-9]{6}\D|7[459][0-9]{6}\D' ;Meditel nums pattern

Global $IamPat='(1[013568][0-9]{6}\D|4[128][0-9]{6}\D)|5[0-589][0-9]{6}\D|6[12678][0-9]{6}\D|7[0-3678][0-9]{6}\D' ;IAM nums pattern

;those patterns match phone numbers in dfferents formats: +212xxxxxxxx or 00212xxxxxxxx or 212xxxxxxxx or 0xxxxxxxx or xxxxxxxx

;\D is added in order that the pattern won't match unexpected phone nums. Ex: without \D the 1st pattern would match in string like "21263265235Sara" two different phone numbers: 12632652 and 63265235, where as it has to match only the last one.

;I have added "nothing" to the source string (see 1st post) in order to match with \D the last phone number even if there are no other character next to it in the original source text.

So now I figured out where the "0x0d" comes from, it comes from \D in the pattern.

I find \D is necessary to not fall in the same problem as in the example above, however I'm falling in an other problem which is the extra "0x0d".

So please can you help me rewriting the pattern or with an extra code snippet to remove "0x0d" from results?

Link to comment
Share on other sites

  • Moderators

ElFarah,

I try to stay well away from StringRegExp unless absolutely necessary! I suggest you start a new thread with "RegExp Help" as the title - that always brings the gurus running!

As a workaround - well, 0x0D is actually @CR, so after you have extracted the number you could run StringRegExpReplace($number, @CR, "") which will give you the number less the errant "0x0d".

I have tested this suggestion with the following code and it seems to work:

#include <Array.au3>

$ini = "test.ini"

$t1 = "012345678" & @CR
IniWrite($ini, "TEST", $t1, "With @CR")

$s1 = StringRegExpReplace($t1, @CR, "")
IniWrite($ini, "TEST", $s1, "After SRE Replace")

$array = IniReadSection($ini, "TEST")
_ArrayDisplay($array)

Interestingly, if you run the code again it keeps adding copies of the incorrect key=value to the .ini file, as it does not recognise the previous instances as valid key names.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thank you for the tips.

Based on your suggestion I found an easy way to do the trick.

Note that is not always an "0x0d", it could be any thing which comes next to the phone number.

So here is the 2nd updated code snippet I'm using right now and works perfectly:

CODE
;[...]

For $i=1 To UBound($iamnum)

$iamnum[$i-1]=StringRegExpReplace($iamnum[$i-1], '\D', '')

$t1="0" & $iamnum[$i-1]

$t2='"' & 'IAM Contact ' & $i & '"'

IniWrite($NewLstFile, "IAM", $t1, $t2)

Next

For $i=1 To UBound($mednum)

$mednum[$i-1]=StringRegExpReplace($mednum[$i-1], '\D', '')

$t1="0" & $mednum[$i-1]

$t2='"' & 'MEDITEL Contact ' & $i & '"'

IniWrite($NewLstFile, "Meditel", $t1, $t2)

Next

Thank you again for your help.

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