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

Quick question, is it possible to stop unpack splitting words?

ie: unpack "(a30)*, $mystring

this splits exactly at 30 chars, whether or not it is in the middle of the string

I would like to split at 30 or the nearest word boundary

Is this possible or do I need to do some voodoo on split?

Replies are listed 'Best First'.
Re: unpack and words (updated)
by haukex (Archbishop) on Mar 24, 2021 at 15:17 UTC
    I would like to split at 30 or the nearest word boundary

    Nearest word boundary before or after the 30 characters? How to ask better questions using Test::More and sample data.

    Update: To answer your question "is it possible to stop unpack splitting words?" - No, unpack isn't meant for that. Probably a regular expression would be best, but as I said above, please be more specific as to what your input and expected output is.

Re: unpack and words
by Marshall (Canon) on Mar 26, 2021 at 04:04 UTC
    As a guess, try this:
    use strict; use warnings; use 5.10.0; use Text::Wrap qw(wrap); $Text::Wrap::columns = 30; my $text =<<'END'; this splits exactly at 30 chars, whether or not it is in the middle of + the string I would like to split at 30 or the nearest word boundary Is this possible or do I need to do some voodoo on split? END say wrap('', '', $text); # first arg is indendation of first line # second arg is indendation of subsequent lin +es # third arg of course the text __END__ Prints: this splits exactly at 30 chars, whether or not it is in the middle of the string I would like to split at 30 or the nearest word boundary Is this possible or do I need to do some voodoo on split?
A reply falls below the community's threshold of quality. You may see it by logging in.