473,651 Members | 2,670 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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/

Jul 23 '05 #1
5 2014
On 9 Dec 2004 11:20:33 -0800, R. Rajesh Jeba Anbiah
<ng**********@r ediffmail.com> wrote:
I could see that it is possible to have hash array using objects like
var hash = {"a" : "1", "b" : "2"};
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';
Couldn't still findout how to declare hash array in Array.
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]
--


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.
Jul 23 '05 #2
Michael Winter wrote:
On 9 Dec 2004 11:20:33 -0800, R. Rajesh Jeba Anbiah
<ng**********@r ediffmail.com> wrote:
I could see that it is possible to have hash array using objects like var hash = {"a" : "1", "b" : "2"};
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:

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.
var hash = {a : '1', b : '2'};

var hash = {}; // or new Object();
hash.a = '1';
hash.b = '2';
Couldn't still findout how to declare hash array in Array.
Why would you want to? There's no benefit. But to answer your

question, you'll have to add the properties one by one:
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? [snip]
--
Signature separators are two dashes followed by a space. You seem to

have omitted the space.


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/

Jul 23 '05 #3
"R. Rajesh Jeba Anbiah" <ng**********@r ediffmail.com> wrote:
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.


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.

Jul 23 '05 #4
Martin Bialasinski wrote:
"R. Rajesh Jeba Anbiah" <ng**********@r ediffmail.com> wrote: <snip> Just as 111111=>aaaaaa. Or 1113=>aaaaaa.

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.
But you can't "untranslat e" it, because your translation function

does not do a unambiguous transformation.

You can't know what the value was that became "aaaaaa". If it was 123, 111111 or 1113. Each of then give "aaaaaa" after translation.


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/

Jul 23 '05 #5
Hi,

Michael Winter wrote:
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]


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

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
4510
by: Alexis Nikichine | last post by:
Hello, It is common knowledge that arrays can be used as hashtables: var color = ; color = 0xFF0000; color = 0x0000FF;
47
5060
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they are not at all. If you are doing *array", then you have to use only integer values for array index, as it was since ALGOL.
22
4601
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last month I had enough of extra proof that the cat doesn't hount mice anymore in more and more situations. And the surrent sicretisme among array and hash is the base for it. I summarized all points in this article:...
35
6621
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
38
5185
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please let's us do not go into an "each dot over i" clarification discussion now - however you want to call - you call it ;-) array contains records of all files in the current dir. array contains records of all subs in the current dir
12
3195
by: wxs | last post by:
Many times we have a bunch of enums we have from either different enums or the same enum that will have various numeric values assigned. Rarely will there be collisions in numbering between the enums. These enums you might imagine would be like OrderPrice=27, OrderQuantity=50, OrderSide=62. There may be a lot of these. So normally what we would have to do is store these in hash table so hashtable=value could be set with the value....
13
2411
by: Sam Kong | last post by:
Hi, I am familiar with the ruby language. To use my ruby knowledge when writing in JavaScript, I use the following library. http://www.advogato.org/proj/Ruby.js/ If I include the file in my html, I can use ruby style methods in JavaScript.
9
1839
by: IamIan | last post by:
I'm using an array to store map features (name, lat, lon, caption, etc), from which the user can then select an individual feature. The problem is that when thousands of features are stored in the array, looping over the entire array looking for a match is SLOW. So I'm running a hash in parallel, where every time a feature is pushed onto the array it's name is also added to the hash as an identical key value pair. I then check if the key...
23
5714
by: raylopez99 | last post by:
A quick sanity check, and I think I am correct, but just to make sure: if you have a bunch of objects that are very much like one another you can uniquely track them simply by using an ArrayList or Array, correct? An example: create the object, create an array, the stuff the object into the array. Later on, assume the object is mutable, the object changes, but you can find it, if you have enough state information to uniquely identify...
0
8352
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8802
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8579
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4283
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2699
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1909
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1587
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.