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

I have a scalar that holds text '$text', I would like to break up this $text into tokens where each token (string) is no larger than 70 characters long. Can anyone help
  • Comment on break up text string into 70 character long tokens

Replies are listed 'Best First'.
Re: break up text string into 70 character long tokens
by gwadej (Chaplain) on Jan 07, 2009 at 19:46 UTC
Re: break up text string into 70 character long tokens
by johngg (Canon) on Jan 07, 2009 at 21:00 UTC

    unpack, as alluded to by zentara.

    $ perl -le ' > $text = q{abcdefghij} x 15; > print for unpack q{(a9)*}, $text;' abcdefghi jabcdefgh ijabcdefg hijabcdef ghijabcde fghijabcd efghijabc defghijab cdefghija bcdefghij abcdefghi jabcdefgh ijabcdefg hijabcdef ghijabcde fghijabcd efghij

    Cheers,

    JohnGG

Re: break up text string into 70 character long tokens
by zentara (Cardinal) on Jan 07, 2009 at 19:27 UTC
Re: break up text string into 70 character long tokens - fun with references
by Narveson (Chaplain) on Jan 07, 2009 at 20:03 UTC

    Open an in-memory filehandle and do a fixed-width read.

    use Data::Dumper; my $text = 'Your stuff goes here!' x 42; # filehandle to read the contents of $text open my $reader, '<', \$text; # read 70 characters at a time local $/ = \70; my @tokens = <$reader>; print "Here are my tokens:\n", Dumper(\@tokens);
Re: break up text string into 70 character long tokens
by kennethk (Abbot) on Jan 07, 2009 at 19:34 UTC

    I'm not 100% on what you are asking, but I read it as you have a variable named $var1 which contains the text '$text' as well as a variable named $text which contains a long string, which you wish to break up according to some rules.

    The first part of the question can be resolved using symbolic references, which are disabled under use strict and are probably not something that you should use.

    The second part should be done with split combined with a regular expression. If you just want to split every 70 characters, it can be done with:

    $var = 'text'; $text = 'This is not a very long string.'; my @array = (); while (${$var}) { push @array, substr ${$var}, 0, 70, undef; }

    Update: Typo fixes.

Re: break up text string into 70 character long tokens
by Bloodnok (Vicar) on Jan 07, 2009 at 20:04 UTC
    @tokens = /(.{0,70})/g; ?? e.g.
    pointo1d@unforgiven:~/workspace/SVC-Class-Utils$ perl -MData::Dumper - +e '@t = "abcdefghijk" =~ /(.{0,2})/g;print Dumper \@t' $VAR1 = [ 'ab', 'cd', 'ef', 'gh', 'ij', 'k', '' ]; pointo1d@unforgiven:~/workspace/SVC-Class-Utils$

    A user level that continues to overstate my experience :-))
      Using the regex  /.{1,$n}/g gets rid of that pesky empty string at the end of the list and also allows variable width.
      >perl -wMstrict -MData::Dumper -le "my $width = shift; my @t = 'abcdefghijklmno' =~ /.{1,$width}/g; print Dumper \@t; " 4 $VAR1 = [ 'abcd', 'efgh', 'ijkl', 'mno' ]; >perl -wMstrict -MData::Dumper -le "my $width = shift; my @t = 'abcdefghijklmno' =~ /.{1,$width}/g; print Dumper \@t; " 3 $VAR1 = [ 'abc', 'def', 'ghi', 'jkl', 'mno' ]; >perl -wMstrict -MData::Dumper -le "my $width = shift; my @t = 'abcdefghijklmno' =~ /.{1,$width}/g; print Dumper \@t; " 2 $VAR1 = [ 'ab', 'cd', 'ef', 'gh', 'ij', 'kl', 'mn', 'o' ];