in reply to split chars in string

Why? Perl has very good string handling that generally works much faster on strings than any code that deals with characters one at a time. Tell us what you want to do and we can probably show you a Perlish way to achieve your end that runs much faster than dealing a character at a time.


True laziness is hard work

Replies are listed 'Best First'.
Re^2: split chars in string
by 7stud (Deacon) on Nov 23, 2009 at 12:49 UTC
    How about sorting an array of words by the second character of every word? Can that be achieved without splitting the words into characters?
      #!/usr/bin/perl use strict; use warnings; use 5.010; chomp( my @words = <DATA> ); @words = sort { substr( $a, 1, 1 ) cmp substr( $b, 1, 1 ) } @words; say for @words; __DATA__ How about sorting an array of words by the second character every word

      Just look at the second character.

      my @sorted_by_second = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [$_, substr($_,1,1)] } @unsorted;