SitePoint Sponsor

User Tag List

Results 1 to 23 of 23
  1. #1
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post Overloading Arrays for multi-dimensional purposes

    VARIABLE1,VARIABLE1,PROPERTY1,PROPERTY2...PROPERTY6|\

    You have a number of lines - in the above format.
    They are in a single variable.
    You want to take it
    and create a multi-dimensional Array.
    MORE than that - you want it more in an hash ( list, dictionary ) format.

    I thought it couldn't be done in JS.
    I finally hit a problem which i couldn't overcome without
    working out how to handle this.

    For anyone that doesn't know how these look:
    Code:
    [VARIABLE 1a]
        | - [VARIABLE 2a] = [ PROPERTY 1, PROPERTY 2 ...]
        | - [VARIABLE 3a] = [ PROPERTY 1, PROPERTY 2 ...]
        | - [VARIABLE 4a] = [ PROPERTY 1, PROPERTY 2 ...]
        | - [VARIABLE 5a] = [ PROPERTY 1, PROPERTY 2 ...]
    [VARIABLE 1b]
        | - [VARIABLE 2b] = [ PROPERTY 1, PROPERTY 2 ...]
        | - [VARIABLE 3b] = [ PROPERTY 1, PROPERTY 2 ...]
        | - [VARIABLE 4b] = [ PROPERTY 1, PROPERTY 2 ...]
        | - [VARIABLE 5b] = [ PROPERTY 1, PROPERTY 2 ...]
    Now you want to be able to access these by name but not
    have multiples, and not overwrite the previous value.

    The method I've ended up with looks like this:
    Code:
    MAKES = new Array();
    TYPES = new Array();
    
    var str='\
    ECMN,AVIS,FORD,MONDEO,GREEN,1.5|\
    ECMN,HERTZ,FORD,MONDEO,GREEN,1.5|\
    ECAR,HERTZ,FORD,MONDEO,GREEN,1.5|\
    ';
    
    var all = str.split(/\|/);
    
    for (i=0;i<all.length;i++){
            var set = all[i].split(/,/);
            if (!TYPES[set[0]]){ TYPES.push(set[0]); TYPES[set[0]] = 1; }
            if (!MAKES[set[1]]){ MAKES.push(set[1]); MAKES[set[1]] = 1; }
            l=set[0]+"['"+set[1]+"']=new Array('"+set[2]+"','"+set[3]+"','"+set[4]+"','"+set[5]+"')";
            try { eval(l); }
            catch(e) { eval(set[0]+' = new Array();'); eval(l); }
            }
    
    alert(eval(TYPES[1]+'["'+MAKES[1]+'"]'));
    alert(MAKES);
    Effectively this gives you the ablility to say:
    alert ( ECMN['AVIS'] ) and you'll get an array returned
    containing Ford Mondeo Green 1.5!

    Now i don't know if i'm being stupid - but to me that
    seems like a very slight breakthrough.

    Am i rediscovering the wheel, and is there a common way of doing this?

    Can anyone think of how to improve this?

    Flawless
    Last edited by Flawless_koder; Jul 26, 2002 at 04:30.
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  2. #2
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now something I *hadn't* thought of was setting the
    length of the array to be the name of the new array name.

    That way - when you call ECMN.length - instead of returning
    0 (although techically it pseudo holds four or so values) - it would return the number of true elements.
    THAT way - you can then use ECMN to query what arrays
    it holds as if it really were a multi-dimensional hash/list/dictionary - RATHER than just a multi-dimensional array.

    The change is with the variable s and the evaluation of it:

    Code:
    <script language="Javascript">
    
    MAKES = new Array();
    TYPES = new Array();
    
    var str='\
    ECMN,Avis,Ford Mondeo 1.6,32.5,1|\
    ECMN,Hertz,Vauxhall Astra 1.5,35.1,1|\
    ECMN,Option,Vauxwagon Jetta 1.4,29.5,1|\
    ECMN,Sixt,Lexus IS300,41.2,0|\
    ECAR,Avis,Mercedez C200,42.1,0|\
    ECAR,Hertz,BMW 318i,45,1|\
    ECAR,Option,Vauxwagon Passat 1.5,27.1,1|\
    ECAR,Budget,Ford Scorpio 1.8,37.2,1|\
    ECAR,Sixt,Skoda Octavia 1.9,35.4,1|\
    CDMN,Avis,Ford Focus 1.3,21.5,1|\
    CDMN,Hertz,Peugeot 207 1.4,25.1,0|\
    CDMN,Budget,Nissan Micra 1.1,16.1,1|\
    CDMN,Option,Vauxhall Corsa 1.2,15.3,1|\
    LDAR,Avis,Mercedez SLK 500,82,1|\
    LDAR,Hertz,BMW 850csi, 95.2,0|\
    LDAR,Sixt,Lexus GS 500, 89.2,1|\
    ';
    
    var all = str.split(/\|/);
    
    for (i=0;i<all.length;i++){
            var set = all[i].split(/,/);
            if (!TYPES[set[0]]){ TYPES.push(set[0]); TYPES[set[0]] = 1; }
            if (!MAKES[set[1]]){ MAKES.push(set[1]); MAKES[set[1]] = 1; }
            l=set[0]+"['"+set[1]+"']=new Array('"+set[2]+"','"+set[3]+"','"+set[4]+"')";
            s=set[0]+"["+set[0]+".length]='"+set[1]+"'";
            try { eval(l); }
            catch(e) { eval(set[0]+' = new Array();'); eval(l); }
            eval(s);
            }
    
    var COMPANIES = new Array();
    var COMPANIES = new Array();
    
    COMPANIES['AVIS'] = new Array('image/r_avis.gif','#FFFFFF');
    COMPANIES['HERTZ'] = new Array('image/r_hertz.gif','#EAEAEA');
    
    alert(ECMN[ECMN[0]]);
    You can see that with ECMN[ECMN[0]] we have direct access
    to the array inside it by calling it's name
    This allows our pseudo for:

    for (i=0;i<ECMN.length;i++){
    ECMN[ECMN[i]] << this recieves the array object.
    }

    NOW - we could either use the default "for variable in object" OR we could create our own constructer with a new function - and call it progressively - though that would
    take a lot of work.

    Is anyone interested in this at all?

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  3. #3
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's fantastic, but like you said...not sure if I have a valid application for it other than testing it on my machine and saying, "CL!"

    What does catch() do?
    Last edited by beetle; Jul 26, 2002 at 06:21.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  4. #4
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well no - i had a need for it - so i created it.

    try {}
    catch (e){}
    is an error handling mechanism.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  5. #5
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so it works like this?

    try { [...snippeta...] }
    catch(e){ [...snippetb...] }


    If 'snippeta' errors then run 'snippetb' and suppress the error.

    Do I have that right?
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  6. #6
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes - and e recieves the error.

    There's more complexities to it if you need them...
    but in this case i didn't.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  7. #7
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Correction - for loops

    Rather than needing to create your own handler for the members - why not just use the for (obj in obj) statement - which isn't based on length!

    This means we can run it with:
    if (!CLASSES[set[0]]) CLASSES.push(set[0]);// CLASSES[set[0]] = 1; }
    if (!COMPS[set[1]]) COMPS.push(set[1]);// COMPS[set[1]] = 1; }

    Rather.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  8. #8
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    or rather:

    if (!CLASSES[set[0]]) CLASSES[set[0]] = 1; }
    if (!COMPS[set[1]]) COMPS[set[1]] = 1; }

    If you're doing it backwards ( name based )

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  9. #9
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,067
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there a way of doing that without using eval?

  10. #10
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ummm - what can i say apart from "HUH?" ..

    Which bit?

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  11. #11
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,067
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The code in your first post on this thread.

  12. #12
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh - you mean - without evaluation the names...

    Well:

    set[0] in one iteration may = 'ECMN'

    if you weren't to evaluate it - it would either
    take set[0] as the value held in it - or the literal of
    the name... either way - not what we'd want.

    BUT - the only reason i'm doing it that way is because
    i'm generating the arrays automatically.

    If you weren't - then you're fine saying:

    array['NAME'] = new Array( ARRAYREF1, ARRAYREF2 ... );

    where ARRAYREFS are arrays to their own right.
    That example takes it to a 4th dimension though - i think.

    In this example you'd end up - for sure - with array.length
    as 0. To iterate you'd have to use:

    for (name in array)

    and in that case name would recieve the literal value NAME -
    so you'd have to then use:
    array[NAME] - to pull up the array referenced - and then indexing ( or a second level of naming ) to pull up the 3rd or 4th dimensions respectively.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  13. #13
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol! How spooky - earlier today I used the "for x in y" statement to finally create a neater solution to my character entity problem than what I was originally going for!!! :-)
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  14. #14
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh post that up, so you actually got a fix for that thing then M@rco

  15. #15
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh and flawless, you should post somemore of your stuff that you've done, i'm sure others will think HTFDHDT (How The F@ck Did He Do This). As i'm still trying to work it out on things like the wire meshes for 3d graphics etc.

  16. #16
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    *blush*

    Why thank you Andrew.

    I'll post some stuff every now and then.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  17. #17
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Andrew - it's not a fix, but it is a better overall solution (in the end) than my original approach. I'll "post up" !!!!
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  18. #18
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Marco - can we take a look at a real fix for your problem then... if you haven't yet found one?

    Flawless
    Last edited by Flawless_koder; Jul 27, 2002 at 07:16.
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  19. #19
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've posted the code in that thread:
    http://www.sitepointforums.com/showt...threadid=67869
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  20. #20
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And get my name right! It's short enough!
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  21. #21
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fixed - - Sorry about that.

    I'm SURE i've heard the second part of that expression before...
    ...

    "Why can't you get it in first time - it's short enough!"



    he he

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  22. #22
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Indeed! lol
    (I don't like to brag, but I've never encountered that problem myself! )
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  23. #23
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    You see i always thought that the suit must have been
    making up for something

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •