This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Regexp help

Help for those learning Tcl or writing their own scripts.
Post Reply
W
Wannabe
Voice
Posts: 17
Joined: Fri Feb 10, 2006 1:02 pm

Regexp help

Post by Wannabe »

Hey guys, im struggling with a script im trying to write, i have a php script which dumps an array into a browser, and a tcl script that parses several bits of information from that array dump.

However, i now have several sections which are identical appart from the stored information im trying to retrieve, and im not sure how to get all matching results, anyone able to explain a method?

What im wanting is the information behind [hostname] and [map] from each section, the array dump is :

Code: Select all

    Array
(
    [nsserver] => Array
        (
            [address] => 194.154.191.10:27015
            [hostname] => Wireplay UK NS [Classictastic][NS + FF on]
            [map] => ns_mineshaft
            [gamedir] => ns
            [gamename] => NS v3.2.0
            [num_players] => 4
            [max_players] => 16
            [protocol] => 47
            [server_type] => d
            [server_os] => l
            [password] => 0
            [mod] => 1
            [mod_info] => http://www.unknownworlds.com/ns/
            [mod_download] => http://www.unknownworlds.com/ns/view?action=files
            [mod_version] => 0
            [mod_size] => -709672960
            [mod_ssonly] => 9
            [mod_customdll] => 0
        )

    [nsserver2] => Array
        (
            [address] => 194.154.191.11:27015
            [hostname] => Wireplay UK NS [Fight Club][CO + Extralevels]
            [map] => co_daimos_beta5
            [gamedir] => ns
            [gamename] => NS v3.2.0
            [num_players] => 0
            [max_players] => 18
            [protocol] => 47
            [server_type] => d
            [server_os] => l
            [password] => 0
            [mod] => 1
            [mod_info] => http://www.unknownworlds.com/ns/
            [mod_download] => http://www.unknownworlds.com/ns/view?action=files
            [mod_version] => 0
            [mod_size] => -709672960
            [mod_ssonly] => 9
            [mod_customdll] => 0
        )

    [nsserver3] => Array
        (
            [address] => 194.154.191.12:27015
            [hostname] => Wireplay UK NS [Melting Pot][CO only]
            [map] => co_angst
            [gamedir] => ns
            [gamename] => NS v3.2.0
            [num_players] => 2
            [max_players] => 18
            [protocol] => 47
            [server_type] => d
            [server_os] => l
            [password] => 0
            [mod] => 1
            [mod_info] => http://www.unknownworlds.com/ns/
            [mod_download] => http://www.unknownworlds.com/ns/view?action=files
            [mod_version] => 0
            [mod_size] => -709672960
            [mod_ssonly] => 9
            [mod_customdll] => 0
        )

    [nsserver4] => Array
        (
            [address] => 194.154.191.13:27015
            [hostname] => Wireplay UK NS [Vets Surgery][NS + FF on]
            [map] => ns_mineshaft
            [gamedir] => ns
            [gamename] => NS v3.2.0
            [num_players] => 2
            [max_players] => 18
            [protocol] => 47
            [server_type] => d
            [server_os] => l
            [password] => 0
            [mod] => 1
            [mod_info] => http://www.unknownworlds.com/ns/
            [mod_download] => http://www.unknownworlds.com/ns/view?action=files
            [mod_version] => 0
            [mod_size] => -709672960
            [mod_ssonly] => 9
            [mod_customdll] => 0
        )

    [nsserver5] => Array
        (
            [address] => 194.154.191.14:27015
            [hostname] => Wireplay UK NS [ProtoLab][CO + PLUGINS]
            [map] => co_core
            [gamedir] => ns
            [gamename] => NS v3.2.0
            [num_players] => 3
            [max_players] => 18
            [protocol] => 47
            [server_type] => d
            [server_os] => l
            [password] => 0
            [mod] => 1
            [mod_info] => http://www.unknownworlds.com/ns/
            [mod_download] => http://www.unknownworlds.com/ns/view?action=files
            [mod_version] => 0
            [mod_size] => -709672960
            [mod_ssonly] => 9
            [mod_customdll] => 0
        )

)
    
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

Something like:

Code: Select all

set myvar [regexp -all -inline {\[hostname\] (.*?)\n\[map\] (.*?)\n} $input]
That's one way to get the data into a var, then you can use foreach or whatever you want to manipulate it. You'll prolly have to clean the data up first tho, with regsub or somn..
W
Wannabe
Voice
Posts: 17
Joined: Fri Feb 10, 2006 1:02 pm

Post by Wannabe »

What i need ideally, is to read the php array into a tcl array, if i can do that, it would be perfect, only im stuggling to think of a way that isnt far beyond my tcl skills.
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

Once the data is in a var, you can do whatever you want with it, including putting it into an array.

Code: Select all

array set hostnames [regexp -inline -all {\[hostname\] (.*?)\n} $input]
array set map [regexp -all -inline {\[map\] (.*?)\n} $input]

or 

array set data [regexp -all -inline {\[hostname\] (.*?)\n\[map\] (.*?)\n} $input]

or

set count 0;set var1 "";set var2 ""
foreach line {$input} {
        if {[regexp {\[hostname\] (.*?)\n\[map\] (.*?)\n} $line fullmatch var1 var2]} {
                  incr count
                  array set myvar "$var1 $var2" "$count"
         }
}
I use something similar to the last example in a quotes script, to count up the total lines and to pull individual matches (hence using the $count var to keep stuffing data into the array and have a running total at the same time.)
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Post by awyeah »

Note: You do not need to specify the -inline switch with -all. The -all switch already gives the number of possible matches.
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
Post Reply