Perl: the Markov chain saw PerlMonks

### Re: how to sort this?

by ikegami (Pope)
 on Mar 25, 2006 at 05:41 UTC ( #539145=note: print w/replies, xml ) Need Help??

in reply to how to sort this?

The following sorts numerically by the number preceeding the 'x', then numerically by the number following the 'x':

```@a = sort { my (\$a1, \$a2) = split(/x/, \$a);
my (\$b1, \$b2) = split(/x/, \$b);
\$a1 <=> \$b1 || \$a2 <=> \$b2      } @a;

Replies are listed 'Best First'.
Re^2: how to sort this?
by bobf (Monsignor) on Mar 25, 2006 at 06:00 UTC

The OP said the elements contain "both digits & characters", which I assume means (one or more digits) followed by (one or more alpha chars) followed by (one or more digits). If that's the case, ikegami's sort routine can be generalized to include the alpha component:

```@a = sort { my (\$a1, \$a2, \$a3) = ( \$a =~ m/(\d+)(\D+)(\d+)/ );
my (\$b1, \$b2, \$b3) = ( \$b =~ m/(\d+)(\D+)(\d+)/ );
\$a1 <=> \$b1 || \$a2 cmp \$b2 || \$a3 <=> \$b3          } @a;

The OP wasn't clear about the specifics, though. If there really is just a single 'x' between the digit portions (and it's always an 'x'), the generalization isn't needed. If by "characters" the OP meant "any character" (as opposed to only alpha chars), then things get more complex.

Create A New User
Node Status?
node history
Node Type: note [id://539145]
help
Chatterbox?
 [stevieb]: well, what happens is the Arduino 'listens' for requests r/w, and does the appropriate thing when it's interrupted based on the 'register' address sent in. It's ugly as it was my first attempt, but I've got great new ideas I'm just sitting. [stevieb]: ...down to implement now. Here's the sketch as it currently sits [shmem]: well I use I2C and SPI and stuff, but creating a pseudo chip looks to me like lot of indirection and memory clutter... not? [choroba]: Are you going to use the device soon? Related to your comment about "not having much time to do a lot of coding"... [stevieb]: sure, but I'm just learning ;) I consider it practice to get a good understanding of what goes on *after* an I2C/SPI request is made [shmem]: ah ok. Gonna read that. but now.... shmem compiles himself into his template [stevieb]: this is my RPi:: distribution overall automation build system physical layout. It doesn't encompass all of my RPi dists yet, ... [stevieb]: ...but I'm getting close. I've been soldering my own PCBs to gain that experience as well, before I have my own manufactured. [stevieb]: heh, nice *template* ;) 'night shmem!

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2017-06-25 22:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How many monitors do you use while coding?

Results (572 votes). Check out past polls.