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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.