Your skill will accomplishwhat the force of many cannot PerlMonks

### Re^3: Custom Sort An AoA

by SuicideJunkie (Vicar)
 on Apr 01, 2014 at 17:43 UTC ( #1080609=note: print w/replies, xml ) Need Help??

in reply to Re^2: Custom Sort An AoA
in thread Custom Sort An AoA

If all the data were numeric and limited in length, you could zero-pad it before the join and then CMP it since <=> will probably explode on longer arrays.

If you need it to be fully generic, you could always break down and make the sort block be a complex sub that loops over the elements and returns once it finds a difference.

```sub sortIt
{
my \$result = @\$a <=> @\$b;
my \$idx = \$#\$a;
while (!\$result && \$idx >=0)
{
\$result = \$a->[\$idx] cmp \$b->[\$idx];
\$idx--;
}
return \$result;
}

Replies are listed 'Best First'.
Re^4: Custom Sort An AoA
by LanX (Bishop) on Apr 01, 2014 at 18:42 UTC
I'd do it too with an extra helper function, but I would keep the first condition out (for readability, flexibility and reuse)

something like

```sub cmp_vec  {
# return either -1,0 or 1 comparing over two equally sized arrays
}

sort { @\$a <=> @\$b or cmp_vec(\$a,\$b) } @list;

for full flexibility a cmp-function could be passed as call-back in third position.

As a side note, I'm not sure if this could be done with Perl6 Meta ops, something like @a >>cmp<< @b (after reversing of course).

Cheers Rolf

( addicted to the Perl Programming Language)

##### update

something like

```sub cmp_vec {
my (\$a,\$b) = @_;
for (reverse 0.. @\$a-1) {
if (my \$x= \$a->[\$_] cmp \$b->[\$_]) {
return \$x
}
}
return 0;
]
LanX,
You may be amused to know that this question is being used to help me identify and organize execution paths through 6502 Assembly. (See also my response to shmem).

Cheers - L~R

Re^4: Custom Sort An AoA
by Limbic~Region (Chancellor) on Apr 01, 2014 at 17:54 UTC
SuicideJunkie,
...and limited in length....

You would also need to know that limit and pad everything at every level or else it would still be very complex.

If you need it to be fully generic...

Ah yes, this is along the lines of what I was looking for.

Cheers - L~R

Ah yes, this is along the lines of what I was looking for.

Although it feels a bit strange spoon-feeding you a solution... ;)

Implementing the orcish maneuver as kennethk suggested is left as an excercise for the reader.

perl -le'print map{pack c,(\$-++?1:13)+ord}split//,ESEL'
shmem,
I almost started the thread with I know this is false laziness but.... You are right that this was well within my capabilities. I am not one to make excuses but I have been completely immersed in 6502 Assembly since last Friday afternoon. In fact, this code was to identify an execution path through some assembly. When the answer didn't immediately pop into my head I decided it was best not to think about it. I knew if I switched contexts from Assembly (which I am just learning) to Perl, the effort to regain where I was would be a lot of work.

Mea culpa

Cheers - L~R

Create A New User
Node Status?
node history
Node Type: note [id://1080609]
help
Chatterbox?
 [thao4]: Hi all, I am new in perl. I need do somthing in perl: exemple: - search a patern "CADRE 1.1 CPU" in one file.txt - incremente +30 lines from the line with the patern found - replace the patern "Model Regle" by "Heath Rule A" in this new line (30 line [marto]: good morning all [hippo]: Welcome. Tell me, have you read perlintro? That's a great place to start. [thao4]: I seach to do that since a week but I can't found the answer in google. Thank for your help [Eily]: hello thao4 (and hello all :D), you should [id://http:// perlmonks.org/ index.pl?node_id= 479#post|open a question on Seekers of Perl Wisdom], be sure to include whatever you have tried that didn't work, or only does the job partially [thao4]: yes, I do a little perl since some years but not fréquently, I can correct the triggers in Clearcase of IBM, but write the code : no [Eily]: meh, such good link ... [Eily]: here [Eily]: still not xD there

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2018-02-21 09:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When it is dark outside I am happiest to see ...

Results (276 votes). Check out past polls.

Notices?