array question

H

Huub

Hi,

In my script I read 4 fields (2 numerical and 4 strings (i.e.
'char'-type)) from a MySQL database to print it. This works well, but I
want to sort the array on one of the numerical fields before printing.
I've looked through CPAN but get lost on looking for array. Can I store
those fields in 1 array (how?) and sort the array or is there another
way I should do this?

Thanks,

Huub
 
J

Jürgen Exner

Huub" <"v.niekerk at hccnet.nl said:
In my script I read 4 fields (2 numerical and 4 strings (i.e.
'char'-type)) from a MySQL database to print it. This works well, but
I want to sort the array

Which array?
on one of the numerical fields before
printing. I've looked through CPAN but get lost on looking for array.

If you want to sort then the documentation for sort() should probably be
your first information source.
Also, there are quite a few tips in the FAQ: "How do I sort an array by
(anything)?"
Can I store those fields in 1 array (how?)

Well, technically yes, but it doesn't make much sense to store disparate
items in the same list. You don't put your shopping list for gorceries, time
for your next oil change, and contact list of your friends on the same sheet
of paper either.
and sort the array or is there another way I should do this?

What about an AoH (aka Array of Hash)?

jue
 
M

Mirco Wahab

Huub said:
In my script I read 4 fields (2 numerical and 4 strings (i.e.
'char'-type)) from a MySQL database to print it. This works well, but I
want to sort the array on one of the numerical fields before printing.
I've looked through CPAN but get lost on looking for array. Can I store
those fields in 1 array (how?) and sort the array or is there another
way I should do this?

What exactly do you do? (Working-)code snippets?

Why isn't it in your case possible to retrieve
sorted records from the sql? Do you have accsess
to the sql command?

SELECT h.firstnumber, h.secondnumber, h.firststring, h.secondstring
FROM huubtable as h
ORDER BY h.secondnumber



BTW:
perldoc -q "How do I sort an array by (anything)?"

Regards

Mirco
 
H

Huub

Jürgen Exner said:
Which array?


If you want to sort then the documentation for sort() should probably be
your first information source.
Also, there are quite a few tips in the FAQ: "How do I sort an array by
(anything)?"


Well, technically yes, but it doesn't make much sense to store disparate
items in the same list. You don't put your shopping list for gorceries, time
for your next oil change, and contact list of your friends on the same sheet
of paper either.


What about an AoH (aka Array of Hash)?

jue

Thank you.
 
H

Huub

Mirco said:
What exactly do you do? (Working-)code snippets?

Why isn't it in your case possible to retrieve
sorted records from the sql? Do you have accsess
to the sql command?

SELECT h.firstnumber, h.secondnumber, h.firststring, h.secondstring
FROM huubtable as h
ORDER BY h.secondnumber

Didn't think of this way.
BTW:
perldoc -q "How do I sort an array by (anything)?"

Thank you.
 
M

Martijn Lievaart

Didn't think of this way.

If the table is large and you create an index on secondnumber, this is
also very fast, at the expense of slower insertion times.

M4
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top