Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: string manipulation

by meraxes (Friar)
on Jan 12, 2009 at 22:27 UTC ( #735823=note: print w/ replies, xml ) Need Help??


in reply to string manipulation

Use split in the form split PATTERN, EXPR, LIMIT

You can tell split the maximum number of fields to split into. If annn, bnnnn etc. are never going to have periods in them it's trivial.

--
meraxes


Comment on Re: string manipulation
Select or Download Code
Re^2: string manipulation
by johngg (Abbot) on Jan 12, 2009 at 23:14 UTC
    ... never going to have periods in them it's trivial.

    Even if they do it's not too difficult if you split the reversed string with a limit.

    use strict; use warnings; my @testStrings = qw{ abc.1.0.1.0 bnnn.1.0.1.1 this.that.1.0.2.0 dots.a.plenty.here.1.0.2.1 }; foreach my $testString ( @testStrings ) { my( $rest, $digits ) = map { scalar reverse( $_->[ 4 ] ), join q{.}, reverse @$_[ 0 .. 3 ] } map { [ split m{\.}, $_, 5 ] } scalar reverse $testString; print qq{$rest -- $digits\n}; }

    The output.

    abc -- 1.0.1.0 bnnn -- 1.0.1.1 this.that -- 1.0.2.0 dots.a.plenty.here -- 1.0.2.1

    I hope this is of interest.

    Cheers,

    JohnGG

      Fair enough. :) I'll see your improvement and raise you this:

      use strict; use warnings; my @testStrings = qw{ abc.1.0.1.0 bnnn.1.0.1.1 this.that.1.0.12.0 dots.a.plenty.here.1.0.2.1 }; foreach my $testString ( @testStrings ) { my @parts = split m{\.}, (scalar reverse $testString), 5; my $rest = scalar reverse $parts[4]; my $digits = join q{.}, map{ scalar reverse $_ } reverse @parts[0..3]; print qq{$rest -- $digits\n}; }

      If the digits could also be > 10, yours will reverse them (like the this.that in this example is 12, yours would print 21).

      --
      meraxes

        Good catch with the reversed digits. I'm peeved with myself because I've done a similar thing before and should have remembered. Corrected code.

        use strict; use warnings; my @testStrings = qw{ abc.1.0.1.0 bnnn.1.0.1.1 this.that.1.0.215.0 dots.a.plenty.here.1.0.21.1 }; foreach my $testString ( @testStrings ) { my( $rest, $digits ) = map { scalar reverse( $_->[ 4 ] ), join q{.}, reverse map { $_ = reverse } @$_[ 0 .. 3 ] } map { [ split m{\.}, $_, 5 ] } scalar reverse $testString; print qq{$rest -- $digits\n}; }

        And output.

        abc -- 1.0.1.0 bnnn -- 1.0.1.1 this.that -- 1.0.215.0 dots.a.plenty.here -- 1.0.21.1

        Cheers,

        JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://735823]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2014-12-27 11:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls