Split by length

Discussion in 'Perl Misc' started by Papago, Sep 3, 2004.

  1. Papago

    Papago Guest

    Is there a single Perl command that will split a string into an array by
    number of characters? For example, if I had the string:

    $string = "monkey";

    I could specify a lenght, such as 2, and it would put every pair of 2
    characters into an array that looks something like this:

    $array[0] = "mo";
    $array[1] = "nk";
    $array[3] = "ey";

    Is that possible?
    Papago, Sep 3, 2004
    #1
    1. Advertising

  2. "Papago" <> wrote:

    > Is there a single Perl command that will split a string into an array by
    > number of characters? For example, if I had the string:
    >
    > $string = "monkey";
    >
    > I could specify a lenght, such as 2, and it would put every pair of 2
    > characters into an array that looks something like this:
    >
    > $array[0] = "mo";
    > $array[1] = "nk";
    > $array[3] = "ey";
    >
    > Is that possible?


    Yes. You can do it the easy way by capturing stuff with a regex or you can do
    it a longer way using substr(). I'm sure there are many other ways as well.
    What way did your teacher want you to use?
    David K. Wall, Sep 3, 2004
    #2
    1. Advertising

  3. Papago

    Fayland Guest

    I wrote some code as folows:
    -----------------------
    $a = "avcdfadad";

    foreach ($a =~ /.{1,2}/g) { # if u donn't want to push the last one char to
    array,use /.{2}/
    push (@a, $_);
    }

    foreach (@a) {
    print "$_\n";
    }

    print scalar @a;
    -----------------------

    It's weird that I use "@a = split(/(.{1,2})/,$a);", it give me 10 elements
    of array.


    "Papago" <> дÈëÓʼþ news:...
    > Is there a single Perl command that will split a string into an array by
    > number of characters? For example, if I had the string:
    >
    > $string = "monkey";
    >
    > I could specify a lenght, such as 2, and it would put every pair of 2
    > characters into an array that looks something like this:
    >
    > $array[0] = "mo";
    > $array[1] = "nk";
    > $array[3] = "ey";
    >
    > Is that possible?
    >
    >
    Fayland, Sep 3, 2004
    #3
  4. Fayland wrote:
    >
    > foreach ($a =~ /.{1,2}/g) { # if u donn't want to push the last one
    > char to array,use /.{2}/
    > push (@a, $_);
    > }


    The foreach loop is redundant:

    @a = $a =~ /.{1,2}/g;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Sep 3, 2004
    #4
  5. Fayland <> wrote:


    > $a = "avcdfadad";



    > It's weird



    No it isn't.

    If you had told us what you thought was weird, we could have perhaps
    explained it.

    At it is, it is doing exactly what is expected, so we cannot help
    repair whatever misunderstanding you have...


    > that I use "@a = split(/(.{1,2})/,$a);", it give me 10 elements
    > of array.



    You get 5 (empty) fields, and 5 separators, just what the function's
    docs say you should.

    That adds up to 10 (and I didn't even have to take off my shoes
    to calculate that :)


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 3, 2004
    #5
  6. Papago

    Fayland Guest

    I read the perlfunc "split" again,and understand why the empty fields
    produced.
    Thanks.And I want to ask weather there is a way to skip the empty
    fields(undef),how to write the regexp?
    not /(.{1,2})/ but what?

    "Tad McClellan" <> дÈëÓʼþ
    news:...
    > Fayland <> wrote:
    >
    >
    > > $a = "avcdfadad";

    >
    >
    > > It's weird

    >
    >
    > No it isn't.
    >
    > If you had told us what you thought was weird, we could have perhaps
    > explained it.
    >
    > At it is, it is doing exactly what is expected, so we cannot help
    > repair whatever misunderstanding you have...
    >
    >
    > > that I use "@a = split(/(.{1,2})/,$a);", it give me 10 elements
    > > of array.

    >
    >
    > You get 5 (empty) fields, and 5 separators, just what the function's
    > docs say you should.
    >
    > That adds up to 10 (and I didn't even have to take off my shoes
    > to calculate that :)
    >
    >
    > --
    > Tad McClellan SGML consulting
    > Perl programming
    > Fort Worth, Texas
    Fayland, Sep 3, 2004
    #6
  7. Papago

    Anno Siegel Guest

    Papago <> wrote in comp.lang.perl.misc:
    > Is there a single Perl command that will split a string into an array by
    > number of characters? For example, if I had the string:
    >
    > $string = "monkey";
    >
    > I could specify a lenght, such as 2, and it would put every pair of 2
    > characters into an array that looks something like this:
    >
    > $array[0] = "mo";
    > $array[1] = "nk";
    > $array[3] = "ey";


    my @l = unpack '(a2)*', $string;

    Anno
    Anno Siegel, Sep 3, 2004
    #7
  8. Fayland <> wrote:
    > "Tad McClellan" <> дÈëÓʼþ
    > news:...
    >> Fayland <> wrote:
    >>
    >>
    >> > $a = "avcdfadad";


    >> > that I use "@a = split(/(.{1,2})/,$a);", it give me 10 elements
    >> > of array.

    >>
    >>
    >> You get 5 (empty) fields, and 5 separators, just what the function's
    >> docs say you should.


    > I read the perlfunc "split" again,and understand why the empty fields
    > produced.
    > Thanks.And I want to ask weather there is a way to skip the empty
    > fields(undef),



    @a = grep length, split(/(.{1,2})/,$a);


    but a m// in list context is Much Better for this task.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 3, 2004
    #8
  9. Papago

    Anno Siegel Guest

    bowsayge <> wrote in comp.lang.perl.misc:
    > Papago said to us:
    >
    > > Is there a single Perl command that will split a string into an array by
    > > number of characters? For example, if I had the string:
    > >
    > > $string = "monkey";
    > >
    > > I could specify a lenght, such as 2, and it would put every pair of 2
    > > characters into an array that looks something like this:
    > >
    > > $array[0] = "mo";
    > > $array[1] = "nk";
    > > $array[3] = "ey";
    > >
    > > Is that possible?

    >
    > Try this:
    >
    > my $string = 'monkey';
    > my $length = 2;
    > my @array = $string =~ m/.{$length}/g;
    > print "@array\n";


    That will ignore a possible remainder of fewer than $length characters
    if the string length isn't divisible by $length. This may be the desired
    behavior, the OP didn't say. In case it isn't, using m/.{1,$length}/g
    will always catch the last piece, even if it's shorter than $length.

    Anno
    Anno Siegel, Sep 3, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    466
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    696
    Alex Martelli
    Sep 17, 2004
  3. trans.  (T. Onoma)

    split on '' (and another for split -1)

    trans. (T. Onoma), Dec 27, 2004, in forum: Ruby
    Replies:
    10
    Views:
    213
    Florian Gross
    Dec 28, 2004
  4. Sam Kong
    Replies:
    5
    Views:
    238
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    604
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page