Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

hash

Thread view: 
Mani - 03 Jun 2006 14:32 GMT
in perl i have the following code,

foreach $word (@words) {      # Loop over each word
       $hash($word)} ++;    # Increment the hash counter for each word
   }

i want to convert the above into java,help please...
i am still not sure about accessing hash of hashes,

that is equivalent code for,
$names{$city}{$account}=0;

-Mani
Lasse Reichstein Nielsen - 03 Jun 2006 14:56 GMT
> in perl i have the following code,
>
[quoted text clipped - 4 lines]
> i want to convert the above into java,help please...
> i am still not sure about accessing hash of hashes,

In Java, you call it a Map, not a hash, and you have to know the
types of the objects.

> that is equivalent code for,
> $names{$city}{$account}=0;

It will probably be:

names.get(city).setAccount(0)

where Name is a class that has a setAccount method, city
is a variable holding a string with a city name, and names
is declared as:
Map<String,Name> names;

In earlier versions of Java, you didn't have generics, so you had
to cast the values extracted from a map, e.g.,
((Name) names.get(city)).setAccount(0)

Good luck.
Signature

Lasse Reichstein Nielsen  -  lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
 'Faith without judgement merely degrades the spirit divine.'

Mani - 03 Jun 2006 16:08 GMT
regarding,
$names{$city}{$account}=0;

in java,
I have declared it as,

Map names=new HashMap();

city and account are declared as Strings.

i tried,

if (account.length() > 0)  names.get(city).put(account, new
Integer(0));
but it shows error.

Also,
$hash($word)} ++;    # Increment the hash counter for each word

how this can be done in java?
where "hash" is the name of the Map.

-Mani

> > in perl i have the following code,
> >
[quoted text clipped - 29 lines]
>  DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
>   'Faith without judgement merely degrades the spirit divine.'
Patricia Shanahan - 03 Jun 2006 16:28 GMT
> regarding,
> $names{$city}{$account}=0;
[quoted text clipped - 17 lines]
> how this can be done in java?
> where "hash" is the name of the Map.

I know Java reasonably well, and have done some Perl programming. They
are very, very different languages. I don't think I could have learned
Perl by trying to translate the way I would do things in Java, and I
don't think you will be able to learn Java by translating Perl thinking,
or by trial and error.

You will be more likely to be successful at producing Java code if you
forget about Perl, and think in terms of learning Java.

What are you really trying to do? In words, not in Perl snippets.

Patricia
Mani - 03 Jun 2006 16:44 GMT
in the code,

$names{$user} {$account} =0

I want to set the value of the element of the hash %{$names{user}}
with key "$account" to the value 0.

-Mani

> > regarding,
> > $names{$city}{$account}=0;
[quoted text clipped - 30 lines]
>
> Patricia
Oliver Wong - 05 Jun 2006 19:06 GMT
[post re-ordered]

>> I know Java reasonably well, and have done some Perl programming. They
>> are very, very different languages. I don't think I could have learned
[quoted text clipped - 13 lines]
> I want to set the value of the element of the hash %{$names{user}}
> with key "$account" to the value 0.

   "%{$names{user}}" isn't a valid variable name in Java. In other words,
you cannot create a variable named "%{$names{user}}" in Java, which means
you cannot set the value of "%{$names{user}}" to anything, so what you ask
is impossible.

   But I'm guessing, the purpose of your program isn't to set the value of
a variable named "%{$names{user}}", but rather something like reserve
airline tickets for clients, or perhaps count the number of occurrences of a
character in a text document.

   That's what is meant when someone tells you to describe the problem in
words, and not in Perl snippets.

   - Oliver
Mani - 05 Jun 2006 20:05 GMT
thanks for ur suggestion!

i am just learning how certain things in perl can be equivalently coded
in java.
not something line by line, but someway of doing that.

in the link,
http://www.cs.mcgill.ca/~abatko/computers/programming/perl/howto/hash/
they have explained how hash of hashes,
and hash of hash of hashes can be done in perl.

i just wanted to know is there any way of doing the same in java.

- Mani

> [post re-ordered]
>
[quoted text clipped - 30 lines]
>
>     - Oliver
Oliver Wong - 05 Jun 2006 20:31 GMT
> thanks for ur suggestion!
>
[quoted text clipped - 8 lines]
>
> i just wanted to know is there any way of doing the same in java.

   You asked about this before, and we've answered you before: What perl
calls "hashes", Java calls "Map". What Perl calls "hash of hashes", Java
calls "Map of Maps". If you don't understand that, then say so in the same
thread, instead of starting a new thread, where the explanation has to start
all over again.

   - Oliver
Patricia Shanahan - 05 Jun 2006 22:45 GMT
>> thanks for ur suggestion!
>>
[quoted text clipped - 16 lines]
>
>    - Oliver

However, note that there are things for which I would use a hash of
hashes in Perl, but a single Map, with a more complicated key class, in
Java.

Designing software in language A and then translating chunks into
language B results in horribly unidiomatic code. I don't write my Perl
code by designing Java code and translating. I write Perl code by
solving the problem in Perl terms.

Patricia
jmcgill - 06 Jun 2006 00:54 GMT
>    You asked about this before, and we've answered you before: What perl
> calls "hashes", Java calls "Map". What Perl calls "hash of hashes", Java
> calls "Map of Maps". If you don't understand that, then say so in the
> same thread, instead of starting a new thread, where the explanation has
> to start all over again.

It is becoming clear that his purpose is not to learn how to do some
specific thing, but rather, to try to persuade others to believe that
perl is somehow generally superior to Java.

The fact that one particular type of dynamic data structure is
implemented tersely in perl syntax, is not a general indication of the
languages superiority.

I'm a huge perl fan, and I love to use it where it fits.  But I would
never seriously attempt to port a highly idiomatic perl routine or
structure directly into another language.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.