> -----Original Message-----
> From: Darin Weeks [mailto:[EMAIL PROTECTED]]
> Sent: Monday, September 24, 2001 4:31 PM
> To: [EMAIL PROTECTED]
> Subject: Comparing two arrays in longest element order
> 
> 
> 
> Hi all,
> 
> I am trying to compare two arrays for a macro building 
> system.  I need to
> start with the longest longest element from array 1 (the 
> macros) and then
> compare it to all elements in array 2.   If a match is found, 
> I replace the
> match with '#0#' (replacing the number with the matching array element
> number).
> 
> Is there a simple way to cycle through the macro array by 
> length of the
> element values WITHOUT permanently reordering the array?
> 
> Below is an example of my macro array and comparison code 
> BEFORE I have
> attempted to do it by length of macro array element.  My 
> thought on how to
> do this would be:
> 
> 1.  loop through the macro array to find the longest element
> 2.  do my replacement work on array #2
> 3.  loop again on array #1 to find the next longest element
> 4.  do next replacement work on #2
> etc....
> 
> Perhaps a better idea would be to create a new array that 
> simply lists the
> order I should use for iteration.
> 
> Once done, I am going to do something similar on the macros 
> themselves to
> collapse similar strings in macro URL's.
> 
> Is there a better way to go about this?  I'm sure there must be.
> 
> Thanks in advance for any advice.
> 
> -- Darin
> 
> HERE IS AN EXAMPLE OF MACRO ARRAY CONTENTS:
> 
> macros[0]{'u'}=http://
> macros[1]{'u'}=http://www.domain.com/
> macros[2]{'u'}=http://www.domain.com/home/
> macros[3]{'u'}=alias=/
> macros[4]{'u'}=alias=/external/
> macros[5]{'u'}=alias=/alias/
> macros[6]{'u'}=http://www.domain.com/bd.redir?redir=
> ...
> 
> sub fURL {
>       for $i ( 0 .. $#$menu ) {
>               for $x ( 0 .. $#macros ) {

This iterates $x over a list of indexes. To process those indexes in
order based on the length of the macros, you could just use sort()
something like this:

for $x (sort {length(macros[$b]{u})<=>length(macros[$a]{u})} 0..$#macros) {

Then $x would be (successively) 6, 2, 1, 4, 5, 0, 3.

The Schwartzian Transform version is

for $x (map { $_->[1] }
        sort { $b->[0] <=> $a->[1] }
        map { [ length(macros[$_]{u}), $_ ] }
        0 .. $#macros) {

>                       if($$menu[$i]{'u'}) {$$menu[$i]{'u'} =~
> s/$macros[$x]{$x}/#$x#/g                      }
>               }
>       }
> }
 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to