Hash array without using objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • R. Rajesh Jeba Anbiah

    Hash array without using objects

    I could see that it is possible to have hash array using objects like
    var hash = {"a" : "1", "b" : "2"};

    Couldn't still findout how to declare hash array in Array.
    var arr = new Array("a" : "1", "b" : "2"); doesn't work. Any hints? TIA

    --
    <?php echo 'Just another PHP saint'; ?>
    Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

  • Michael Winter

    #2
    Re: Hash array without using objects

    On 9 Dec 2004 11:20:33 -0800, R. Rajesh Jeba Anbiah
    <ng4rrjanbiah@r ediffmail.com> wrote:
    [color=blue]
    > I could see that it is possible to have hash array using objects like
    > var hash = {"a" : "1", "b" : "2"};[/color]

    It's not strictly a hash table. What you have above is an object literal
    that contains a property/value list. The following snippets are equivalent:

    var hash = {a : '1', b : '2'};

    var hash = {}; // or new Object();
    hash.a = '1';
    hash.b = '2';
    [color=blue]
    > Couldn't still findout how to declare hash array in Array.[/color]

    Why would you want to? There's no benefit. But to answer your question,
    you'll have to add the properties one by one:

    var arr = []; // or new Array();

    arr.a = '1';

    or

    arr['b'] = '2';

    [snip]
    [color=blue]
    > --[/color]

    Signature separators are two dashes followed by a space. You seem to have
    omitted the space.

    [snip]

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • R. Rajesh Jeba Anbiah

      #3
      Re: Hash array without using objects

      Michael Winter wrote:[color=blue]
      > On 9 Dec 2004 11:20:33 -0800, R. Rajesh Jeba Anbiah
      > <ng4rrjanbiah@r ediffmail.com> wrote:
      >[color=green]
      > > I could see that it is possible to have hash array using objects[/color][/color]
      like[color=blue][color=green]
      > > var hash = {"a" : "1", "b" : "2"};[/color]
      >
      > It's not strictly a hash table. What you have above is an object[/color]
      literal[color=blue]
      > that contains a property/value list. The following snippets are[/color]
      equivalent:

      Thanks for pointing out that. When I quickly searched for hash array
      implementation in JS, I came across that. Didn't know that it's object
      and properties.
      [color=blue]
      > var hash = {a : '1', b : '2'};
      >
      > var hash = {}; // or new Object();
      > hash.a = '1';
      > hash.b = '2';
      >[color=green]
      > > Couldn't still findout how to declare hash array in Array.[/color]
      >
      > Why would you want to? There's no benefit. But to answer your[/color]
      question,[color=blue]
      > you'll have to add the properties one by one:[/color]

      Yes, it doesn't work--both Array and ordinary objects work same in
      hash thing. I'd thought in Array, we can use reverse() method and
      length property as I need to reverse the hash array--but to my
      surprise, it doesn't work--even though Array has reverse() method.

      Basically, I want to translate certain characters and implementing
      something like PHP's strtr <http://in.php.net/strtr>. In one occasion,
      I need to translate characters using map table in forward order, but in
      other case need to do in reverse order. I'd thought that hash
      implementation would be better in this case as it provides better
      readability, etc. In JS, forward thing works fine, but couldn't reverse
      such hash array. Any idea?[color=blue]
      > [snip]
      >[color=green]
      > > --[/color]
      >
      > Signature separators are two dashes followed by a space. You seem to[/color]
      have[color=blue]
      > omitted the space.[/color]

      It's not me, but stupid GG Beta; I'm tired of reporting bugs as they
      don't care about anything. Sounds like they're saving bits and bytes to
      offer GB for their Gmail;)

      --
      <?php echo 'Just another PHP saint'; ?>
      Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

      Comment

      • Martin Bialasinski

        #4
        Re: Hash array without using objects

        "R. Rajesh Jeba Anbiah" <ng4rrjanbiah@r ediffmail.com> wrote:
        [color=blue]
        > I'd thought in Array, we can use reverse() method and length
        > property as I need to reverse the hash array--but to my surprise, it
        > doesn't work--even though Array has reverse() method.[/color]

        Sure reverse() works. It just does not work the way you thing it would.

        a = new Array(1,2,3); // a[0] = 1, a[1] = 2, a[2] = 3
        a.reverse( ); // now a[0] = 3, a[1] = 2, a[2] = 1

        You can't swap indices and values (if this is what you want to do) in
        an Array(), because indices can only be interger and values can be of
        any type.

        Comment

        • R. Rajesh Jeba Anbiah

          #5
          Re: Hash array without using objects

          Martin Bialasinski wrote:[color=blue]
          > "R. Rajesh Jeba Anbiah" <ng4rrjanbiah@r ediffmail.com> wrote:[/color]
          <snip>[color=blue]
          > Just as 111111=>aaaaaa. Or 1113=>aaaaaa.[color=green]
          > >
          > > But, if I use the *same* translation map to "untranslat e" "aaaaaa",
          > > it becomes:
          > > aaaaaa=>111111
          > >
          > > _unless_ I change the order of map and reverse the string.[/color]
          >
          > But you can't "untranslat e" it, because your translation function[/color]
          does[color=blue]
          > not do a unambiguous transformation.
          >
          > You can't know what the value was that became "aaaaaa". If it was[/color]
          123,[color=blue]
          > 111111 or 1113. Each of then give "aaaaaa" after translation.[/color]

          Yes, you're right. Reversing the map table wouldn't help for
          ambiguous transformation. Thanks for pointing out.

          --
          <?php echo 'Just another PHP saint'; ?>
          Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

          Comment

          • Laurent Bugnion

            #6
            Re: Hash array without using objects

            Hi,

            Michael Winter wrote:
            [color=blue]
            > Why would you want to? There's no benefit. But to answer your question,
            > you'll have to add the properties one by one:
            >
            > var arr = []; // or new Array();
            >
            > arr.a = '1';
            >
            > or
            >
            > arr['b'] = '2';
            >
            > [snip][/color]

            Note however that creating an array is not useful in this case, since
            you don't use any of the Array features, but only (mis)use the Array as
            an Object to store properties in it. There is no added value in using an
            Array for this.

            HTH,

            Laurent
            --
            Laurent Bugnion, GalaSoft
            Software engineering: http://www.galasoft-LB.ch
            Private/Malaysia: http://mypage.bluewin.ch/lbugnion
            Support children in Calcutta: http://www.calcutta-espoir.ch

            Comment

            Working...