Jump to content

Changing string in an array


Recommended Posts

Hi all,

I am a little stuck with my arrays.

I have an array which is information pulled from a .ini file

I need to change some characters into words:

^ - CTRL

! - ALT

+ - SHIFT

# - WINDOWS KEY

I have tried loads of different code and ended up at the following, but it fails to work and I have "code blindness" from staring at it for a few days now.

Func HelpTranslate()
Local $strHK = IniReadSection($strINIFile,"HotKeys")
If @error Then
MsgBox(4096, "", "Error occured, section not found." & @LF & @LF & "You work out why.")
Else
_ArrayDisplay($strHK, "Before")
For $i = 0 To UBound($strHK) - 1
ConsoleWrite("Testing " & $strHK[$i] & @CRLF)
_ArrayDisplay($strHK, "After")
Next
EndIf
EndFunc

Here's the .ini for the curious:

[HotKeys]

VNCNew=^!v

VNCOld=^+v

NotepadKey=#n

CMDKey=#`

PasteSig=^!s

PasteOPR=^!a

Link to comment
Share on other sites

JustReidy,

Your code is doing nothing to change the chars. Work out the format you want and use "stringreplace" (see help file) to update each element of the array within the loop. You can alsoupdate the ini file once your code runs sucessfully.

The is a small example of this

Func HelpTranslate()
Local $strHK = IniReadSection($strINIFile, "HotKeys")
If @error Then
MsgBox(4096, "", "Error occured, section not found." & @LF & @LF & "You work out why.")
Else
_ArrayDisplay($strHK, "Before")
For $i = 0 To UBound($strHK) - 1
$strHK[$i] = StringReplace($strHK[$i], '^', 'CTRL') ; this changes all "^" to "CTRL" in this element
ConsoleWrite("Testing " & $strHK[$i] & @CRLF)
_ArrayDisplay($strHK, "After")
Next
EndIf
EndFunc ;==>HelpTranslate

kylomas

edit: Note Michael's comment below. Did not know that this was a 2D array. Origional comment stands, however. You are not updating anything.

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Hi Buddy,

IniReadSection give you a 2D array not a 1D array. Read the IniReadSection help :)

This is the example I copy-paste from the help file, this should give you a hint:

Local $var = IniReadSection("C:\Temp\myfile.ini", "section2")
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
Link to comment
Share on other sites

JustReidy,

Re-posting code adjusting for 2D array.

;
;

Func HelpTranslate()
    Local $strHK = IniReadSection($strINIFile, "HotKeys")
    If @error Then
        MsgBox(4096, "", "Error occured, section not found." & @LF & @LF & "You work out why.")
    Else
        _ArrayDisplay($strHK, "Before")
        For $i = 0 To UBound($strHK) - 1
            $strHK[$i][1] = StringReplace($strHK[$i][1], '^', 'CTRL')  ; this changes all "^" to "CTRL"
            ConsoleWrite("Testing " & $strHK[$i][1] & @CRLF)
            _ArrayDisplay($strHK, "After")
        Next
    EndIf
EndFunc   ;==>HelpTranslate

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

JustReidy,

Re-posting code adjusting for 2D array.

;
;

Func HelpTranslate()
    Local $strHK = IniReadSection($strINIFile, "HotKeys")
    If @error Then
        MsgBox(4096, "", "Error occured, section not found." & @LF & @LF & "You work out why.")
    Else
        _ArrayDisplay($strHK, "Before")
        For $i = 0 To UBound($strHK) - 1
            $strHK[$i][1] = StringReplace($strHK[$i][1], '^', 'CTRL') ; this changes all "^" to "CTRL"
            ConsoleWrite("Testing " & $strHK[$i][1] & @CRLF)
            _ArrayDisplay($strHK, "After")
        Next
    EndIf
EndFunc ;==>HelpTranslate

kylomas

Oh god! So simple!

I forgot entirely I was using a 2D array, never had the [1] in "$strHK[$i]" When using StringReplace before.

Works like a charm now, thankyou guys.

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