Arrays and you!

Here you can find plenty of help with your general coding needs and projects
If it is about a specific program, use the corresponding forum
DO NOT ask basic questions such as "How do I print a variable", use Google for that

Arrays and you!

Postby InsaneMind » Tue Dec 04, 2007 4:24 pm

I was wondering if someone would mind taking the time to answer a few questions about arrays and how to get particular data from them. (i.e. CharacterProfiler.lua)

I'm attempting to create a concise list of how to do certain actions so that other users can stumble across the forum and possibly use this information to create their own mods (as I am working on in my spare time).

As many of you probably already know the general structure of the CharacterProfile.lua file is always the same. You can view arrays as a tier or series of tiers to get to your information much like stairs in a house...you can stop on the 2nd floor to see the bedroom or go to the 3rd floor and use the bathrom.

Using lua2phparray.php or some other form of lua parser you can "parse" the lua file into a php array. Once you do that the return will be a giant array with many small arrays on the inside. the first tier of that array in our case is rpgoCPpref generally and myProfile second.

now using...

Code: Select all
foreach(array_keys($myArray) as $realm_name)
    {
    
$realm $myArray[$realm_name];
    
print_r(array_keys($myArray));
    echo 
"<br><br>";
    }
 


which outputs the following

Code: Select all
Array ( [0] => rpgoCPpref [1] => myProfile )

Array ( [
0] => rpgoCPpref [1] => myProfile )  


is there a way to get and store the values rpgoCPpref and myProfile without knowing that they are called that...and stored in a variable able to be echo'd out without an array.

I ask this because the next few tiers are character names that are dynamic and are not consistant with each user.

I've attempted to look for several ways to do this but I'm not sure on exactly what to do to get those values.

Anyone mind giving me a hand?


Thanks
Insane
Last edited by InsaneMind on Tue Dec 04, 2007 4:25 pm, edited 1 time in total.
InsaneMind
WR.net Apprentice
WR.net Apprentice
 
Posts: 6
Joined: Fri Nov 30, 2007 11:34 pm

Re: Arrays and you!

Postby tuigii » Tue Dec 04, 2007 8:30 pm

InsaneMind wrote:is there a way to get and store the values rpgoCPpref and myProfile without knowing that they are called that...and stored in a variable able to be echo'd out without an array.

I ask this because the next few tiers are character names that are dynamic and are not consistant with each user.


Well, as you already figured out, this array exist IRL, and is being parsed by the roster.
So, why not looking at at file update.lib.php, starting from line 288:
Code: Select all
    function processMyProfile()
    {
        global 
$roster;

        
$output '';
        
$myProfile $this->uploadData['characterprofiler']['myProfile'];

        
$this->resetMessages();

        foreach( 
$myProfile as $realm_name => $realm)
        {
            
$this->current_realm $realm_name;

            if( isset(
$realm['Character']) && is_array($realm['Character']) )
            {
                
$characters $realm['Character'];

                
// Start update triggers
                
if( $roster->config['use_update_triggers'] )
                {
                    
$output .= $this->addon_hook('char_pre'$characters);
                }

                foreach( 
$characters as $char_name => $char)
                {
                
// Code hidden : CP version test and Character handling here.....
                
}
                    
$output .= "<br />\n";
                }

                
// Start update triggers
                
if( $roster->config['use_update_triggers'] )
                {
                    
$output .= $this->addon_hook('char_post'$characters);
                }
            }
            else
            {
                
$output .= $roster->locale->act['noGuild'];
            }
        }
        return 
$output;
    } 


Note that the code does search for each Character in the ['Character'] array with the (snipped in the code above) :
Code: Select all
    foreach( $characters as $char_name => $char


That's the power of the foreach php command. You don't need to know the index name(s) in the array.
In the loop, you'll find it in $char_name.
User avatar
tuigii
WR.net Master
WR.net Master
 
Posts: 891
Joined: Wed Dec 27, 2006 12:57 pm
Location: Somewhere in the South Ouest of France

Arrays and you!

Postby InsaneMind » Tue Dec 04, 2007 9:36 pm

I've looked at this over and over but I didn't have the knowledge behind some of commands...such as.


foreach( $myProfile as $realm_name => $realm)

How is this read? For each array $myProfile as each key $realm_name has a value....do this (is that correct?)

Next I've seen this input a lot but I'm not sure how it works... $this->...I'm not sure of the abilities of that statement or it's scope (I can't find it on php.net anywhere).

Thanks for the help...the more I understand it the better I get, sorry for even more questions


***Edit***
I was able to find out that $this-> is a class and I'm looking more into how it's used in wowroster to help me better understand what a class is and what it can do.
Last edited by InsaneMind on Wed Dec 05, 2007 1:30 am, edited 2 times in total.
InsaneMind
WR.net Apprentice
WR.net Apprentice
 
Posts: 6
Joined: Fri Nov 30, 2007 11:34 pm

Arrays and you!

Postby PleegWat » Wed Dec 05, 2007 3:14 am

If you haven't done so yet, read the first couple of chapters of the PHP manual on http://www.php.net/manual/en/ to get a grip on the language's base structure.
I <3 /bin/bash
User avatar
PleegWat
WoWRoster.net Dev Team
WoWRoster.net Dev Team
 
Posts: 1636
Joined: Tue Jul 04, 2006 1:43 pm

Re: Arrays and you!

Postby tuigii » Wed Dec 05, 2007 3:35 am

InsaneMind wrote:... to help me better understand what a class is and what it can do.


That's was my question, back in 1985, when I had a look at a language named Smalltalk that just came out. After that, it was C++ all over the place.

To make a long story very short : it has to do with this famous Object Oriented language structures. Understand them well - and you'll be driving a Porsch real soon.

Note : I'm still NOT driving a Porsche :wink:
User avatar
tuigii
WR.net Master
WR.net Master
 
Posts: 891
Joined: Wed Dec 27, 2006 12:57 pm
Location: Somewhere in the South Ouest of France

Re: Arrays and you!

Postby InsaneMind » Thu Dec 06, 2007 4:27 am

PleegWat wrote:If you haven't done so yet, read the first couple of chapters of the PHP manual on http://www.php.net/manual/en/ to get a grip on the language's base structure.



Yup been reading a bit here and there to grasp onto things
InsaneMind
WR.net Apprentice
WR.net Apprentice
 
Posts: 6
Joined: Fri Nov 30, 2007 11:34 pm

Re: Arrays and you!

Postby InsaneMind » Thu Dec 06, 2007 4:35 am

tuigii wrote:...back in 1985, when I had a look at a language named Smalltalk that just came out. After that, it was C++ all over place...




Tuigii - Thanks, your original post started to make sense the more I dove into it over and over again.



Now I believe I'll be able to do exactly what I need to do but my problem will rely on optimizing what i'm trying to do...that will come later...first functionality :)
InsaneMind
WR.net Apprentice
WR.net Apprentice
 
Posts: 6
Joined: Fri Nov 30, 2007 11:34 pm


Return to General Code Help

Who is online

Users browsing this forum: No registered users and 0 guests

cron