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

Hi,

This is my input data :-

12345678 123456789

and I want to split each two variables by space, as example :-

12 34 56 78 12 34 56 78 9

Could somebody guide me how could i do that ?

Edited (davorg): removed code tags from non-code sections

Replies are listed 'Best First'.
Re: how to split input data ?
by thezip (Vicar) on Nov 09, 2006 at 01:59 UTC

    This should do what you're looking for:

    #!/usr/bin/perl use strict; use warnings; my $data = "123456789"; # get two digits, and replace them with two digits # and a space (globally repeating until the end of # the string) $data =~ s/(\d{2})/$1 /g; print $data, "\n";

    jd

    Where do you want *them* to go today?
      Yeah.. this is i am looking for.... Thank you very much....

        You're welcome!

        Where do you want *them* to go today?
Re: how to split input data ?
by GrandFather (Saint) on Nov 09, 2006 at 01:33 UTC

    What have you tried? you could use split with a little mucking about, but it would be easier to use a regular expression.


    DWIM is Perl's answer to Gödel
      does the look-behind count as mucking around? ;) (as opposed to having to shift off the first blank element)
      perl -le 'print join ":", split /(?<=\d\d)(\d\d)/, "123456789"'
      oh, i guess that doesn't work for '123' ... back to a little more mucking:
      perl -le 'print join ":", grep {length} split /(\d\d)/, "123456789"'
      Currently, I am dump all the input data into array, then split it for +each two variables. I think that is not a good method. Do you know the simple solution ?

        Show us your current code and we will comment on that. That way we have some idea of what you know already.


        DWIM is Perl's answer to Gödel
Re: how to split input data ?
by davido (Cardinal) on Nov 09, 2006 at 03:01 UTC

    I would do it like this. You'll notice that it's similar to a previous solution, but takes the extra step of intentionally failing to match at the end of the string. That way you don't end up with "89_" (where I'm using _ to signify trailing space).

    In my example, I'm intentionally printing a '#' at the end of each string to demonstrate that there's no extra whitespace being appended to the end of the string.

    use strict; use warnings; my @data = ( '12345678', '123456789' ); foreach my $number ( @data ) { my $modified = $number; $modified =~ s/(\d{2})(?!$)/$1 /g; print "$modified#\n"; }

    Dave

Re: how to split input data ?
by Moriarty (Abbot) on Nov 09, 2006 at 01:48 UTC

    You could also use substr and loop through until you run out of characters.

Re: how to split input data ?
by jwkrahn (Abbot) on Nov 09, 2006 at 06:08 UTC
    Another way to do it (without regular expressions):
    $ echo "12345678 123456789" | perl -lne'print join q[ ], unpack q[(a2)*], $_' 12 34 56 78 12 34 56 78 9

      Or without even Perl . . .

      $ irb -r enumerator irb(main):001:0> "12345678".split(//).enum_slice( 2 ).inject( [] ) { | +a,x| a << x.join( "" ) }.join( " " ) => "12 34 56 78" irb(main):002:0> "123456789".split(//).enum_slice( 2 ).inject( [] ) { +|a,x| a << x.join( "" ) }.join( " " ) => "12 34 56 78 9"

      Update: Or maybe a little more concisely:

      irb(main):011:0> "12345678".split(//).enum_slice( 2 ).map {|a| a.join( +"")}.join(" ") => "12 34 56 78"
Re: how to split input data ?
by Firefly258 (Beadle) on Nov 09, 2006 at 19:36 UTC
    Don't we all get through this one every so often? exploring some other possibilities ..
    perl -le 'print $1 while ($ARGV[0] =~ s/(.{1,2})(.*)/$2/g);' "12345678 +9" perl -le '$c=-2; print while $_ = substr $ARGV[0], $c+=2, 2;' "1234567 +89" # or perl -le 'while ($_ = substr $ARGV[0], $c, 2) { print; $c+=2 };' "1234 +56789" perl -le '@_=reverse split//, $ARGV[0]; while (@_) {print join"", (pop +@_, pop@_);}' "123456789" perl -le '@_=split//, $ARGV[0]; while (@_) {print join"", (shift@_, sh +ift@_);}' "123456789" perl -le '@_=split//, $ARGV[0]; while (@_) {print join "", splice @_, +0, 2}' "123456789" perl -le '@_ = split //,$ARGV[0]; splice @_, $_, 2, $_[$_].$_[$_+1] fo +r 0..$#_/2; print "@_"' "123456789" # some alternatives to ones already posted.. perl -le 'print for grep /./, split /(..)/, $ARGV[0]' "123456789" perl -le 'print for unpack "(Z2)*", $ARGV[0]' "123456789" # probably the fastest echo 123456789 | perl -nle 'print for /(..?)/g'