Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > array question

Reply
Thread Tools

array question

 
 
Huub
Guest
Posts: n/a
 
      03-18-2007
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
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      03-18-2007
Huub" <"v.niekerk at hccnet.nl wrote:
> 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


 
Reply With Quote
 
 
 
 
Mirco Wahab
Guest
Posts: n/a
 
      03-18-2007
Huub wrote:

> 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
 
Reply With Quote
 
Huub
Guest
Posts: n/a
 
      03-18-2007
Jürgen Exner wrote:
> Huub" <"v.niekerk at hccnet.nl wrote:
>> 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
>
>


Thank you.
 
Reply With Quote
 
Huub
Guest
Posts: n/a
 
      03-18-2007
Mirco Wahab wrote:
> Huub wrote:
>
>> 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
>


Didn't think of this way.

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


Thank you.
 
Reply With Quote
 
Martijn Lievaart
Guest
Posts: n/a
 
      03-18-2007
On Sun, 18 Mar 2007 14:41:26 +0100, Huub wrote:

> Mirco Wahab wrote:
>> Huub wrote:
>>
>>> 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
>>
>>

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
Question concerning array.array and C++ Fabio Python 0 11-05-2008 03:53 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
"array of Derived" is not a kind-of "array of Base" question Joseph Turian C++ 11 01-19-2005 11:53 AM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57