hash

M

Mani

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
 
L

Lasse Reichstein Nielsen

Mani said:
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,

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.
 
M

Mani

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
 
P

Patricia Shanahan

Mani said:
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.

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
 
M

Mani

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
 
O

Oliver Wong

[post re-ordered]

Mani said:
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.

"%{$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
 
M

Mani

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


Oliver said:
[post re-ordered]

Mani said:
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.

"%{$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
 
O

Oliver Wong

Mani said:
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.

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
 
P

Patricia Shanahan

Oliver said:
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

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
 
J

jmcgill

Oliver said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,167
Latest member
SusanaSwan
Top