Jump to content
Sign in to follow this  
AustrianOak

StringSplit with multiple delimiters

Recommended Posts

AustrianOak

I have a string in my ini file that looks like this:

text|123;text|456;text|789

and I am looking for a way only to extract the string "text" from the ini file.

I tried something like this:

$var = INIRead(@ScriptDir & "\temp.ini, "section", "key", "")
$split = StringSplit($var, ";"); i first split by semi-colon
$split2 = StringSplit($split[1], "|"); then i split by the "|" characters
For $i = 1 To $split2[0]
    MsgBox(0, "", $split2[$i])

msgbox() only returns the first text in the ini, it doesn't return all three..

where did i go wrong?

Edited by nowagain

Share this post


Link to post
Share on other sites
SmOke_N

#include <array.au3>
$var = "text|123;text|456;text|789"
$aSRE = StringRegExp($var, "(?s)(?m:^|;)(.+?)\|", 3)
_ArrayDisplay($aSRE)


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
AustrianOak

what if my ini starts out in a different format than what i told you.

it starts out usually in this format:

key=text;text;text

how would stringregexp() need to be changed?

Share this post


Link to post
Share on other sites
SmOke_N

what if my ini starts out in a different format than what i told you.

it starts out usually in this format:

key=text;text;text

how would stringregexp() need to be changed?

Use weaponx's version or do some experiments on your own... Once you've exhausted yourself on trying to figure it out, post all that you've tried to receive assistance with RegExp's.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
weaponx

what if my ini starts out in a different format than what i told you.

it starts out usually in this format:

key=text;text;text

how would stringregexp() need to be changed?

If you are using IniRead then "key=" will no longer be part of your string, it will only return "text;text;text"....

Share this post


Link to post
Share on other sites
Xenobiologist

Do you really mean only text, or are there other word than just text?


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites
martin

I have a string in my ini file that looks like this:

text|123;text|456;text|789

and I am looking for a way only to extract the string "text" from the ini file.

I tried something like this:

$var = INIRead(@ScriptDir & "\temp.ini, "section", "key", "")
$split = StringSplit($var, ";"); i first split by semi-colon
$split2 = StringSplit($split[1], "|"); then i split by the "|" characters
For $i = 1 To $split2[0]
    MsgBox(0, "", $split2[$i])

msgbox() only returns the first text in the ini, it doesn't return all three..

where did i go wrong?

A bit late but I notice that no one actually answered your question.

You went wrong here

For $i = 1 To $split2[0]

It should have been

For $i = 1 To $split[0]


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
AustrianOak

there are other words than just text.

its like:

value1;value4

if thats what you were asking, its not the same exact string called "text"

Share this post


Link to post
Share on other sites
AustrianOak

It should have been

For $i = 1 To $split[0]

But then how would it have split by "|" this if it wasn't $split2[0]?

Edit: I think the easiest thing to do is to go with weaponx's way.

anyway...im getting some sleep, been up 24 hours...

Edited by nowagain

Share this post


Link to post
Share on other sites
Xenobiologist

#include <array.au3>

$var = "text|123;text|456;text|789"

$aSRE = StringRegExp($var, "\w+", 3)

_ArrayDisplay($aSRE)

;or

$aSRE = StringRegExp($var, "[^\|;0-9]+", 3)

_ArrayDisplay($aSRE)


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites
crashdemons

process('text|123;text|456;text|789')

Func process($string)
    $delim1=StringSplit($string,';')
    $max=UBound($delim1)-1
    For $i=1 To $max
        $delim2=StringSplit($delim1[$i],'|')
        $text=$delim2[1]
        $number=$delim2[2]
        
        ;;Do Whatever Here
        MsgBox(0,'','Entry #'&$i&@CRLF&'Text: '&$text&@CRLF&'Number: '&$number)
        ;;Do Whatever Here
        
    Next
EndFunc

Edited by crashdemons

My Projects - WindowDarken (Darken except the active window) Yahsmosis Chat Client (Discontinued) StarShooter Game (Red alert! All hands to battlestations!) YMSG Protocol Support (Discontinued) Circular Keyboard and OSK example. (aka Iris KB) Target Screensaver Drive Toolbar Thingy Rollup Pro (Minimize-to-Titlebar & More!) 2D Launcher physics example Ascii Screenshot AutoIt3 Quine Example ("Is a Quine" is a Quine.) USB Lock (Another system keydrive - with a toast.)

Share this post


Link to post
Share on other sites
AustrianOak

@crashdemons:

look at this:

process('text|123;text|456;text|789')

Func process($string)
    $delim1=StringSplit($string,';')
    $max=UBound($delim1)-1
    For $i=1 To $max
        $delim2=StringSplit($delim1[$i],'|')
        $text=$delim2[1]
        $number=$delim2[2]
       
       ;;Do Whatever Here
        MsgBox(0,'','Entry #'&$i&@CRLF&'Text: '&$text&@CRLF&'Number: '&$number)
       ;;Do Whatever Here
       
    Next
EndFunc

msgbox(0, "", $text)

its gives me an error that $text is "undeclared" when i put it in the msgbox()?

what if i only want that function to return just $text so i can used it "outside" the function.

what do i have to do?... :)

Share this post


Link to post
Share on other sites
weaponx

@crashdemons:

look at this:

process('text|123;text|456;text|789')
 
 Func process($string)
     $delim1=StringSplit($string,';')
     $max=UBound($delim1)-1
     For $i=1 To $max
         $delim2=StringSplit($delim1[$i],'|')
         $text=$delim2[1]
         $number=$delim2[2]
        
       ;;Do Whatever Here
         MsgBox(0,'','Entry #'&$i&@CRLF&'Text: '&$text&@CRLF&'Number: '&$number)
       ;;Do Whatever Here
        
     Next
 EndFunc
 
 msgbox(0, "", $text)

its gives me an error that $text is "undeclared" when i put it in the msgbox()?

what if i only want that function to return just $text so i can used it "outside" the function.

what do i have to do?... :)

The scope of $text is limited to that function, move your call to msgbox...

Share this post


Link to post
Share on other sites
AustrianOak

I dont understand what you mean by move my call.

Share this post


Link to post
Share on other sites
weaponx

I dont understand what you mean by move my call.

You need to learn about variable scope first:

http://www.autoitscript.com/autoit3/docs/i...g_variables.htm

Variables defined within a function are considered local to said function (unless declared Global), meaning the variable does not exist outside the extents of Func...EndFunc.

Share this post


Link to post
Share on other sites
AustrianOak

Well having that said then all I should have to do is add Global for $text in the process()

So I now I have no clue as how to use the $text variable...

Edited by nowagain

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  

×

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.