Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

sorting with substrings

by JFarr (Sexton)
on May 25, 2006 at 21:35 UTC ( [id://551715]=perlquestion: print w/replies, xml ) Need Help??

JFarr has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,
I'm still new to Perl and have a sorting question.
I've tried everything I can think of, and have come up with nothing.
I have an array, consisting of
workstation_1_1, workstation_1_2, voiceserver_1_2, voiceserver_1_1 ect....

Now I would like to be able to sortthis array by the
digits so my output would be
workstation_1_1
voiceserver_1_1
workstation_1_2
voiceserver_1_2

I know how to do this in C++ and Java, but with Perl....NO IDEA.
Is there such an thing as a struct in PERL?
Thanks

Replies are listed 'Best First'.
Re: sorting with substrings
by bpphillips (Friar) on May 25, 2006 at 21:58 UTC
    I'm no C++ or Java hacker but I'm pretty sure Perl's equivalent to those languages' struct is the hash (you might want to take a look at perldata). I'm not sure how useful they would be from the description of your problem though. I'm not sure whether the following is the most straightforward for you to understand at this point in your Perl skill-level, but this is how I might solve the problem (with the caveat that I'm taking your description of the problem very literally and that you have no control over the source data and your description of the sorting was complete):
    my @data = qw( workstation_1_1 workstation_1_2 voiceserver_1_2 voiceserver_1_1 ); my @sorted = map { join('_', @{$_} ) # put the data back together again } sort { $a->[2] <=> $b->[2] # first sort by the final digit || $b->[0] cmp $a->[0] # then by the name, descending } map { # seperate the data out into seperate "fields" for easy # sorting (typically called a "Schwartzian Transform") [ split(/_/,$_) ] } @data; # the operation starts here (believe it or not)
    If you're not familiar with map and sort, essentially, they act like "pipes" or "filters" that pass list data from the right side to the left, while giving you the option to perform some operation on each element of the list before you pass it on. So, it might help to start on the last line of the above example and follow the @data as it flows upwards into @sorted.

    HTH
    -- Brian
Re: sorting with substrings
by borisz (Canon) on May 25, 2006 at 22:08 UTC
    my @q = qw(workstation_1_1 workstation_1_2 voiceserver_1_2 voiceserver_1_1); @q = map { join '_', @$_ } sort { $a->[1] <=> $b->[1] or $a->[2] <=> $b->[2] or $a->[0] cmp $b->[0] } map { [ split /_/ ] } @q; use Data::Dumper; print Dumper( \@q ); __OUTPUT__ 'voiceserver_1_1', 'workstation_1_1', 'voiceserver_1_2', 'workstation_1_2'
    Boris
      Thank both of you for you help.
Re: sorting with substrings
by salva (Canon) on May 26, 2006 at 08:28 UTC
    use Sort::Key::Multi qw(ii_keysort); # ii => two integer keys my @sorted = ii_keysort { /(\d+)_(\d+)$/ } @data;
Re: sorting with substrings
by McDarren (Abbot) on May 25, 2006 at 22:52 UTC
    Update: Oh dear, this is what you get for posting first thing in the morning when you're still half-asleep. I just re-read your question and realised that the below doesn't actually give the desired output :(

    Sort::Naturally will handle this job, and is certainly a much simpler solution.

    #!/usr/bin/perl -w use strict; use Sort::Naturally; my @unsorted = <DATA>; my @sorted = nsort(@unsorted); print @sorted; __DATA__ workstation_1_1 voiceserver_1_1 workstation_1_2 voiceserver_1_2

    Gives...

    voiceserver_1_1 voiceserver_1_2 workstation_1_1 workstation_1_2

    Note that while this is much simpler than the other solutions, it may not necessarily be the most efficient. If you're only dealing with a relatively small dataset this probably isn't a concern. Otherwise, you might need to do some Benchmarking.

    Hope this helps,
    Darren :)

Re: sorting with substrings
by blazar (Canon) on May 26, 2006 at 10:48 UTC
    I'm still new to Perl and have a sorting question.

    A very similar question has been asked only a few days ago.

    Is there such an thing as a struct in PERL?.

    There's something similar both in the language called Perl and in the implementation of that language you're using, called perl. In PERL, I don't know! ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://551715]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-25 13:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found