in reply to Logic for sorting of this given array

I suppose Zaxo's code won't work for you, because you used the array to describe a nested structure with sub-departments and such. In general, if you describe nested structures, use nested structures.
Have a look at the following code; I think it should solve your problem:
#!perl use strict; use warnings; sub returnParsed { my ($array, $index, $level) = @_; my $levelhash = {}; while ($index < @$array) { last if ($array->[$index]->{'level'} < $level); if ($array->[$index]->{'level'} == $level) { $levelhash->{$array->[$index]->{'department'}} = returnParsed($array, $index + 1, $level + 1); } ++$index; } return $levelhash; } sub dumpSorted { my ($structure, $indent, $startindent) = @_; my $dumped = ''; for (sort keys %$structure) { $dumped .= " " x $startindent . "$_\n"; $dumped .= dumpSorted($structure->{$_}, $indent, $startindent + +$indent); } return $dumped; } my @array = ({ level => 1, department => 'Office of President', }, { level => 2, department => 'Recruiting', }, { level => 2, department => 'Software', }, { level => 3, department => 'MIS', }, { level => 3, department => 'System Operations', }, { level => 2, department => 'Pipe line', }, ); print dumpSorted(returnParsed(\@array, 0, 1), 3, 3);
Hope this helped.
CombatSquirrel.
Entropy is the tendency of everything going to hell.

Replies are listed 'Best First'.
Re: Re: Logic for sorting of this given array
by ravish (Acolyte) on Aug 29, 2003 at 05:20 UTC
    THanks for the help, it is usefull. But i have another problem that i need to pass more parameters in my array along with the level and department. it could be more than 3-4 now my array is like this : my @array = ({ level => 1,department => 'Office of President', linkdetails = "something"},
    { level => 2, department => 'Recruiting',linkdetails = "something1" },
    { level => 2, department => 'Software',linkdetails="something2"},
    etc.....
    how will i pass all these information along with the department ?. Actually i want to make on resultant array of
    hashes that will contain all the values so that i can
    pass to HTML template abd can print there. So level is
    also needed so that while printing in HTML page i can multiply the level into number of spaces.
    my resultant array of hashes after sorting look like this :

    my @array = ({ level => 1, department => 'Office of President',},
    { level => 2, department => 'Pipe line',link="",},
    { level => 2, department => 'Recruiting',link=""},
    { level => 2, department => 'software',link="" },
    { level => 3, department => 'MIS',link="" },
    { level => 3, department => 'System Operations',link="" },
    );

    need ur help in this ... Thanks
      Some pieces of advice:
      • Enclose array definitions, etc. in <code>-blocks
      • It's not link="", but link => "", or you'll get a syntax error
      • Capitalisation increases legibility ;-)
      To the code:
      #!perl use strict; use warnings; use Data::Dumper; sub returnParsedRec { my ($array, $index, $level) = @_; my $levelhash = {}; my $otherfields; while ($index < @$array) { last if ($array->[$index]->{'level'} < $level); if ($array->[$index]->{'level'} == $level) { $otherfields = {}; for (keys %{$array->[$index]}) { $otherfields->{$_} = $array->[$index]->{$_} unless ($_ eq 'level' or $_ eq 'department'); } $levelhash->{$array->[$index]->{'department'}} = [ $otherfields, returnParsedRec($array, $index + 1, $lev +el + 1) ]; } ++$index; } return $levelhash; } sub unParseRec { my ($levelhash, $result, $level) = @_; my $tmphash; for (sort keys %$levelhash) { $tmphash = $levelhash->{$_}->[0]; $tmphash->{'department'} = $_; $tmphash->{'level'} = $level; push @$result, $tmphash; unParseRec($levelhash->{$_}->[1], $result, $level + 1); } return $result; } sub returnParsed { return returnParsedRec(shift, 0, 1); } sub unParse { unParseRec(shift, [], 1); } my @array = ( { level => 1,department => 'Office of President', linkdetails => "some +thing"}, { level => 2, department => 'Recruiting',linkdetails => "something1" } +, { level => 2, department => 'Software', linkdetails => "something2"},) +; my $struct = returnParsed(\@array); print Dumper($struct); #print Dumper \@array; print "\n" . "=" x 70 . "\n\n"; print Dumper(unParse($struct));
      Be aware that HTML should not be formatted with spaces, look for lists (<ul>, <ol>, <dl>) and tables.
      Hope this helped.
      CombatSquirrel.
      Entropy is the tendency of everything going to hell.