Re: Sorting based on string value
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 4 Dec 2006 15:14:30 -0800
Ninja67 wrote:
I've got the following code in a script:
my $cost_order = "A2yB";
my @series_cost = ();
for (@{$struct->{tree}->[0]->{rectangle}}) {
my $rec_string = "<table><tr><td ";
$rec_string .= ' meascode="'.$_->{meascode}.'"></td></tr></table>';
SWITCH: {
if ($_->{datafile} =~ /^cost/) { push(@series_cost, $rec_string);
last SWITCH; }
$nothing = 1;
}
}
Now that I have my array filled, I want to sort the contents of each
array by the "meascode" attribute. Each meascode attribute is a single
character that matches one of the characters in the $cost_order string.
How can I sort an array based on the order of the characters in the
$cost_order string?
So my array might contain:
<table><tr><td meascode="y"></td></tr></table>
<table><tr><td meascode="2"></td></tr></table>
<table><tr><td meascode="A"></td></tr></table>
<table><tr><td meascode="B"></td></tr></table>
but I need it to contain:
<table><tr><td meascode="A"></td></tr></table>
<table><tr><td meascode="2"></td></tr></table>
<table><tr><td meascode="y"></td></tr></table>
<table><tr><td meascode="B"></td></tr></table>
Maybe I'm not understanding your goal, but why aren't you just sorting
the values in your original array, before looping through them and
putting them inside strings in a new array? Why would you want to fill
the array, convoluting your value, then have to loop back through that
string, parse out the data you just put in, so that you can compare it?
Change:
for (@{$struct->{tree}->[0]->{rectangle}}) {To:
for (sort { $a->{meascode} cmp $b->{meascode} }
@{$struct->{tree}->[0]->{rectangle}}) {
Paul Lalli
.
- References:
- Sorting based on string value
- From: Ninja67
- Sorting based on string value
- Prev by Date: Re: Printing out 3 dimensional arrays.
- Next by Date: Re: PS1 shell variable
- Previous by thread: Sorting based on string value
- Next by thread: Re: Sorting based on string value
- Index(es):
Relevant Pages
|