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

I'm attempting to "split" a multi-line string into a string of a specific length. The length of the original string is variable. The first thing that I thought of is using a combo of substr and s/// to do this.

For some reason though, the below goes into an infinite loop. Can anybody explain why? Point me towards a better solution (non-modular, I know all about Text::Autoformat).
$description =~ s/^(\s+)?$//g; #get rid of blank lines $description =~ s/\n/ /g; #get rid of new lines while ($description) { my $temp = substr($description,0,75); #get first 75 $description =~ s/$temp//; #strip the 75 $f_desc .= "T" . $temp . "\n"; #put a newline at the end } #when the loop is done, $f_desc should hold the parsed description

Replies are listed 'Best First'.
Re: substr to split lines?
by japhy (Canon) on Aug 28, 2001 at 23:06 UTC
    Your first regex needs some help: s/^\s*$//mg will remove "lines" that consist of only whitespace.

    Your loop fails because using s/// is a poor idea here. If $temp contains any regex characters, you're busted. You want to use the four argument substr().

    while (length $description) { $f_desc .= "T" . substr($description, 0, 75, '') . "\n"; }
    That returns the 75 characters and removes them from $description.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: substr to split lines?
by thraxil (Prior) on Aug 28, 2001 at 22:34 UTC

    you need to put a '/s' on those first two regexps to get them to work across multiple lines:

    $description =~ s/^(\s+)?$//sg; #get rid of blank lines $description =~ s/\n/ /sg; #get rid of new lines

    update:

    in addition, changing the $description =~ s/$temp//; to substr($description,0,75) = "" seems to fix it and is probably better form.

    anders pearson

      Oops... No. The regex modifier /s only effects matching of '.' (dot). (And ignores $* -- but don't worry about that.) the_slycer's problem lies elsewhere.
      I'm not sure how your solution is to work with what I need to do though. (Probably because of my lack of understanding with regards to substr).

      The way the code reads now is:
      $description =~ s/^(\s+)?$//sg; $description =~ s/\n/ /sg; while ($description) { $f_desc .= "T".(substr($description,0,75) = "")."\n"; } print $f_desc;
      However, when given a description like the following (which is what I am working with) it doesn't come out quite right.
      2001/08/28 10:15:54 AM auser 08/28/01 10:15:53 auser PostalCode.24th floor. MachineID, printer keeps jamming (A person' +s Name). Lexmark optra s1250. 2001/08/28 10:17:57 AM auser PostalCode.24th floor. MachineID, printer keeps jamming (A person's N +ame). Lexmark optra s1250.
      The above code makes it read like:
      T floor. MachineID, printer keeps jamming (A person's Name). Lexmark +optra s125 T0. 2001/08/28 10:17:57 AM auser PostalCode.24th floor. MachineID, +printer kee Tps jamming (A person's Name). Lexmark optra s1250. T
      So it looks like I'm missing the first line, and I'm not sure why :-)
      UPDATE: Japhy got it. That's why he's the saint and I'm the lowly whatever I am now :-)