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

I have the following variables:- $a=abc.1.0.1.0 $b=bnnn.1.0.1.1 $c=dnnnnn.1.0.2.0 ....etc........ i want to split the variable $a, $b, $c and put that value in two sepa +rate varaibles. for example $a is splitted to $a2 = 1.0.1.0 (always the last four digits of the string) $a1 = abc (Rest of the String) $b is splitted to $b2 = 1.0.1.1 (always the last four digits of the string) $b1 = bnnn (Rest of the String) i am trying to get the results with a for loop Can anyone please help?

Replies are listed 'Best First'.
Re: string manipulation
by meraxes (Friar) on Jan 12, 2009 at 22:27 UTC

    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
      ... 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
Re: string manipulation
by Lawliet (Curate) on Jan 12, 2009 at 22:27 UTC

    What is the problem here? You have a job that must be done and you have a a plan to complete the job. It seems like all you need to do is code.

    Read How do I post a question effectively? How (Not) To Ask A Question. Then reread it. Read it a third time, too. Perhaps just memorize the whole thing.

    Update: Upon reading the reply posted above me I thought I may have been too harsh -- maybe you are just unaware of Perl's functions. This thought was then forgotten (only to be remembered as of writing this) when I saw your node Array problem......PLEASE HELP!. Clearly, you know enough to get the job done.

    And you didn't even know bears could type.

        Oh, I didn't know there was another version to link to. The one that is listed in the PM FAQ is the one by SiteDocClan, not you. There must be some reason as to why they made a copy of your node, though. Maybe it just looks more official or something.

        Anyway, I'll update my Free Nodelet to link to your node rather than the other ;D

        And you didn't even know bears could type.

Re: string manipulation
by swampyankee (Parson) on Jan 13, 2009 at 15:36 UTC

    If your strings are always going to be formatted in the same way,it's easy:

    #!/usr/bin/perl use strict; use warnings; while(<DATA>) { chomp; (my $text, my $numerical) = split(/\./,$_,2); print "$text: \n"; print "$numerical: \n"; } __DATA__ abc.1.0.1.0 bnnn.1.0.1.1 dnnnnn.1.0.2.0

    Note that this assumes there is a period separating the text part from the numerical part (it looks like a version number to me ;-) ). I also almost always chomp input, mostly because I want to have explicit control over when \n's get inserted and not rely on end-of-record markers in input data.


    Information about American English usage here and here. Floating point issues? Please read this before posting. — emc

Re: string manipulation
by sanku (Beadle) on Jan 13, 2009 at 10:18 UTC
    There is way to split the above variables as follows
    @array=("abc.1.0.1.0","bnnn.1.0.1.1","dnnnnn.1.0.2.0"); foreach (@array){ if($_=~/(\w+)\.(\d{1,2}\.\d{1,2}\.\d{1,2}\.\d{1,2})/) { print "$1 :::::: $2\n"; } }