Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Alphnumeric Matching

by mrbbq (Sexton)
on Jan 30, 2012 at 23:39 UTC ( [id://950845]=perlquestion: print w/replies, xml ) Need Help??

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

Hello, I am trying to wrap my head around alphanumeric sorting and I have tried many of the things mentioned in past threads but I am still not having any luck. I want to eject tapes in order, they are labeled as such: B0001R B1001R B4356R etc. I have some code that I use to eject them now but they are not sorted and I want to sort them pre-eject so they come out in order. Below is the code:

$ejected_tapes=0 ; #my @tapes_to_eject=sort @_ ; #Below works fine but tapes are not sorted my @tapes_to_eject=@_ ; #my @tapes_to_eject=sort { substr($a, 1) <=> substr($b, 1)} @_ +; # Test #print "@_"; if ( $ejected_tapes == $maxslots ) { print "The export door in robot $robot_num only holds +$maxslots tape(s), \n"; print "and we have now ejected that many tapes. Please + go and empty the\n"; print "import/export doors and press <RETURN> when you +'re finished"; $ready=<STDIN>; $ejected_tapes=0; } `/usr/openv/volmgr/bin/vmchange -res -multi_eject -w - +rn $robot_num -rt tld -rh $robot_host -ml $\@_[0]:$\@_[1]:$\@_[2]:$\@ +_[3]:$\@_[4]:$\@_[5]:$\@_[6]:$\@_[7]:$\@_[8]:$\@_[9]:$\@_[10]:$\@_[11 +]:$\@_[12]:$\@_[13]:$\@_[14]:$\@_[15]:$\@_[16]:$\@_[17]:$\@_[18]:$\@_ +[19]:$\@_[20]:$\@_[21]:$\@_[22]:$\@_[23]:$\@_[24]:$\@_[25]:$\@_[26]:$ +\@_[27]:$\@_[28]:$\@_[29]:$\@_[30]:$\@_[31]:$\@_[32]:$\@_[33]:$\@_[34 +]:$\@_[35]:$\@_[36]:$\@_[37]:$\@_[38]:$\@_[39]:$\@_[40]:$\@_[41]:$\@_ +[42]:$\@_[43]:$\@_[44]:$\@_[45]:$\@_[46]:$\@_[47]:$\@_[48]:$\@_[49]:$ +\@_[50]:$\@_[51]` ; }

Thanks for looking and sorry if this is noob sauce.

Replies are listed 'Best First'.
Re: Alphnumeric Matching
by GrandFather (Saint) on Jan 31, 2012 at 00:00 UTC

    If you are having trouble with sort how about you show us the sort code that is failing, the data you are feeding it, and how you expect the sorted result to look? The following may be a suitable starting point:

    use strict; use warnings; my @tapes_to_eject = qw{B1001R B0001R B4356R}; my $tapesStr = join ':', map {"\$$_"} sort @tapes_to_eject; print "$tapesStr\n";

    Prints:

    $B0001R:$B1001R:$B4356R
    True laziness is hard work
Re: Alphnumeric Matching
by kielstirling (Scribe) on Jan 31, 2012 at 00:05 UTC
    Sorry,

    I'm finding it hard to follow your question and example code.

    Will something like the following work? If not please explain yourself more?

    #!/usr/bin/perl -w use strict; my @labels = qw(B0001R B1001R B4356); for my $label (sort @labels) { print "$label\n"; }

      Hi, thanks for the response. The @tapes_to_eject array has the tape numbers but the are not in order.

      $ejected_tapes=0 ; #my @tapes_to_eject=sort @_ ; my @tapes_to_eject=@_ ; #my @tapes_to_eject=sort { substr($a, 1) <=> substr($b, 1)} @_ +; # Test #print "@_";

      Then I run them through the eject command

      `/usr/openv/volmgr/bin/vmchange -res -multi_eject -w -rn $robot_num +-rt tld -rh $robot_host -ml $\@_[0]:$\@_[1]:$\@_[2]:$\@_[3]:$\@_[4]:$ +\@_[5]:$\@_[6]:$\@_[7]:$\@_[8]:$\@_[9]:$\@_[10]:$\@_[11]:$\@_[12]:$\@ +_[13]:$\@_[14]:$\@_[15]:$\@_[16]:$\@_[17]:$\@_[18]:$\@_[19]:$\@_[20]: +$\@_[21]:$\@_[22]:$\@_[23]:$\@_[24]:$\@_[25]:$\@_[26]:$\@_[27]:$\@_[2 +8]:$\@_[29]:$\@_[30]:$\@_[31]:$\@_[32]:$\@_[33]:$\@_[34]:$\@_[35]:$\@ +_[36]:$\@_[37]:$\@_[38]:$\@_[39]:$\@_[40]:$\@_[41]:$\@_[42]:$\@_[43]: +$\@_[44]:$\@_[45]:$\@_[46]:$\@_[47]:$\@_[48]:$\@_[49]:$\@_[50]:$\@_[5 +1]` ;

      The problem the tape numbers in @tapes_to_eject are not sorted. Thanks for looking

Re: Alphnumeric Matching
by Util (Priest) on Jan 31, 2012 at 16:06 UTC

    In all the code that you posted (both commented-out and uncommented), you populate @tapes_to_eject, but never *use* @tapes_to_eject. In your vmchange command, you use @_ directly (although obtusely).

    You must either:

    1. Update @_ with a sorted copy of the tape list, or
    2. Use @tapes_to_eject instead of @_ in the vmchange command.

    Here is how I would approach the problem:

    #!perl use strict; use warnings; # Global vars: my $robot_num = 42; # Faked my $robot_host = 'localhost'; # Faked my $max_slots = 52; # Derived from original post. my $live_eject = 0; # for testing eject_tapes( qw( B4356R B0001R B1001R ) ); sub sort_tapes { my @tapes = @_; return sort { substr($a, 0, 1) cmp substr($b, 0, 1) or substr($a, 1, 4) <=> substr($b, 1, 4) or substr($a, 5, 1) cmp substr($b, 5, 1) } @tapes; } # This simpler version might be sufficient. #sub sort_tapes { return sort @_; } sub eject_tapes { warn "No tapes passed to eject_tapes()" and return if not @_; my @tapes_to_eject = sort_tapes(@_); while (@tapes_to_eject) { # splice() removes $max_slots tapes from # the front of @tapes_to_eject. my @batch_of_tapes = splice @tapes_to_eject, 0, $max_slots; my $tapes_joined_list = join ':', @batch_of_tapes; my @command = ( qw( /usr/openv/volmgr/bin/vmchange -res -multi_eject -w ), -rn => $robot_num, -rt => 'tld', -rh => $robot_host, -ml => $tapes_joined_list, ); if ($live_eject) { `@command`; } else { print "@command\n"; } # If any tapes remain for another batch of ejections, # then alert the operator and wait. if (@tapes_to_eject) { print <<"END_OF_MESSAGE"; The export door in robot $robot_num only holds $max_slots tape(s), and we have now ejected that many tapes. Please go and empty the import/export doors and press <RETURN> when you are finished. END_OF_MESSAGE <STDIN>; # Waits for <RETURN> } } }

Re: Alphnumeric Matching
by choroba (Cardinal) on Jan 30, 2012 at 23:57 UTC
    Can you show the tape names in the desired order?

      Hi Thanks for the response, the tape list is already in the arrary @tapes_to_eject. Then i tried to sort them like this:

      #my @tapes_to_eject=sort @_ ; my @tapes_to_eject=@_ ; #my @tapes_to_eject=sort { substr($a, 1) <=> substr($b, 1)} @_ +; # Test #print "@_";

      Then I run them through the eject command:

      `/usr/openv/volmgr/bin/vmchange -res -multi_eject -w -rn $robot_num - +rt tld -rh $robot_host -ml $\@_[0]:$\@_[1]:$\@_[2]:$\@_[3]:$\@_[4]:$\ +@_[5]:$\@_[6]:$\@_[7]:$\@_[8]:$\@_[9]:$\@_[10]:$\@_[11]:$\@_[12]:$\@_ +[13]:$\@_[14]:$\@_[15]:$\@_[16]:$\@_[17]:$\@_[18]:$\@_[19]:$\@_[20]:$ +\@_[21]:$\@_[22]:$\@_[23]:$\@_[24]:$\@_[25]:$\@_[26]:$\@_[27]:$\@_[28 +]:$\@_[29]:$\@_[30]:$\@_[31]:$\@_[32]:$\@_[33]:$\@_[34]:$\@_[35]:$\@_ +[36]:$\@_[37]:$\@_[38]:$\@_[39]:$\@_[40]:$\@_[41]:$\@_[42]:$\@_[43]:$ +\@_[44]:$\@_[45]:$\@_[46]:$\@_[47]:$\@_[48]:$\@_[49]:$\@_[50]:$\@_[51 +]` ;

      but the do not come out sorted

        #my @tapes_to_eject=sort { substr($a, 1) <=> substr($b, 1)} @_;
        Well, if you outcomment the sort there's little chance it will sort. And you may want to do a string compare instead of a numeric compare.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2024-04-25 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found