can I trust on key %hash natural order?

F

filippo

Hi,

I load my hash with my order, am I sure that the order is preserved on
command 'foreach (key %myHash)?

BR,

Filippo
 
I

Ignoramus7096

Hi,

I load my hash with my order, am I sure that the order is preserved on
command 'foreach (key %myHash)?

it is actually not preserved. But you could say

foreach (sort keys %myHash)

or

foreach (reverse sort keys %myHash)

i
 
P

Paul Lalli

filippo said:
I load my hash with my order, am I sure that the order is preserved on
command 'foreach (key %myHash)?

Quite the opposite. You can assume that the order is very much not
preserved.

What is the *actual* problem you are trying to solve?

Paul Lalli
 
F

filippo

Paul Lalli ha scritto:
What is the *actual* problem you are trying to solve?

Thanks to everibody for the fast answer.

I have to calculate the bill for renting bungalow. The price depends on
the date:
DATE PRICE_LEVEL
mid june -> low price
mid jul -> med price
mid aug -> high
mid sept -> mid
mig oct -> low

I use Date::Calc to compare dates. Since my prices and dates are stored
into database, I have to fetch all data (date, price_level). I fetch
the records ordered by date and I store them into arrayref
(fetchall_arrayref) or hash_ref. My algorithm must read the records
from array (or hashes) ordered by data.

Another question: if an array is ordered, the command foreach (@array)
keeps the order?

Thanks,
Filippo
 
J

John Bokma

filippo said:
Paul Lalli ha scritto:


Thanks to everibody for the fast answer.

I have to calculate the bill for renting bungalow. The price depends on
the date:
DATE PRICE_LEVEL
mid june -> low price
mid jul -> med price
mid aug -> high
mid sept -> mid
mig oct -> low

I use Date::Calc to compare dates. Since my prices and dates are stored
into database, I have to fetch all data (date, price_level).

Better: do the comparision with SQL. It can compare dates.
Another question: if an array is ordered, the command foreach (@array)
keeps the order?

Yes, but IMO you are putting your logic in the wrong place.
 
F

filippo

John Bokma ha scritto:
Better: do the comparision with SQL. It can compare dates.

this would be the best. Is is possible to embed this

get TIMESTAMP from database (current local date)
get DATE from a database table
compare DATE to TIMESTAMP
return the result of comparison

as an SQL query?

Thanks,

Filippo
 
B

Ben Morrow

Quoth "filippo said:
John Bokma ha scritto:


this would be the best. Is is possible to embed this

get TIMESTAMP from database (current local date)
get DATE from a database table
compare DATE to TIMESTAMP
return the result of comparison

as an SQL query?

This Is Not A Perl Question.

In general, yes, but how depends on which database you are using. Read
its SQL docs.

Ben
 
J

John Bokma

filippo said:
John Bokma ha scritto:


this would be the best. Is is possible to embed this

get TIMESTAMP from database (current local date)

Yup, for example with MySQL that is NOW().
get DATE from a database table
compare DATE to TIMESTAMP
return the result of comparison

as an SQL query?

Yup, but this is an SQL question. If you are working with a database it's
very good to read the manual, especially since this is extremely basic
stuff.

A common mistake I see very often is to extract a lot of data from a
database, and have the program do a lot of manipulation that could be done
by the database itself, and probably much more efficient.
 
J

jl_post

Ciao, filippo!

filippo scrisse:
I load my hash with my order, am I sure that the order is preserved on
command 'foreach (key %myHash)?

No. Hashes are not guaranteed to return the keys in the order they
were made. The keys() function returns the keys in the order most
convenient to the hash, which is almost never the same as the order
most convenient to you.

If you want to get the keys in the order they were inserted, run
this command at your DOS/Unix prompt:

perldoc -q "keep my hash sorted"

It'll tell you to look at the Tie::IxHash module, which is a special
module that allows you to create hashes that remember what order their
keys were added in -- and so will return the keys in that order when
you call the keys() function.

Buona fortuna, filippo.

-- Jean-Luc
 
F

filippo

John Bokma ha scritto:
A common mistake I see very often is to extract a lot of data from a
database, and have the program do a lot of manipulation that could be done
by the database itself, and probably much more efficient.

this is interesting. Actually this was my deliberate coose from the
possible two:

a)
- query all data from database
- cycle on this data

b) query database into the cycle

I choosed A because I though it was best to minimize the database load
and query overhead.

Interesting.

Filippo
 
F

filippo

(e-mail address removed) ha scritto:
perldoc -q "keep my hash sorted"

It'll tell you to look at the Tie::IxHash module, which is a special

snip

thanks, probably I'll use this or I'll change the structure of my
query&cycle routine to cycle&query, letting the database to manage data
calculation.

Ciao,

Filippo
 
I

Ignoramus15879

John Bokma ha scritto:


this is interesting. Actually this was my deliberate coose from the
possible two:

a)
- query all data from database
- cycle on this data

b) query database into the cycle

I choosed A because I though it was best to minimize the database load
and query overhead.

I am not sure what you mean, but, in my experience, letting the
database compile statistics (counts, averages etc) is better than
selecting everything and doing it in perl. Database servers (my
experience mostly centers around MySQL) are much more efficient in
coming up with good query plans, than I used to think.

i
 
J

John Bokma

filippo said:
John Bokma ha scritto:


this is interesting. Actually this was my deliberate coose from the
possible two:

a)
- query all data from database
- cycle on this data

b) query database into the cycle

I choosed A because I though it was best to minimize the database load
and query overhead.

I was thinking about c)

Let de database create the data you want
get this data
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top