in reply to substr question

Ah.... color me stupid, but what is wrong with: ($string) = $string =~ /^(.{100,}?)\b/;

Replies are listed 'Best First'.
Re^2: substr question
by ikegami (Patriarch) on Jun 19, 2010 at 00:57 UTC

    I don't see how that's ever useful. If the string has less than 100 characters, it returns nothing. If the string has 100 or more characters, it returns either the same as the OP's solution or too many characters. (I'm assuming 100 is a maximum, such as a screen width or a field width.)

    You've also used a different definition of "word" than everyone else such that cutting "don't" into "don" and "'t" is acceptable, and so is cutting "don't" into "don'" and "t".

      Ah... the original post said 'around 100 characters' not that that was the maximum. But no matter.

      Sigh. I suppose I did commit the cardinal sin of posting Sloppy code.

      And, I should have been clear that what I posted was NOT a turnkey solution but a suggestion that a regex approach might make sense.

      so... OK, below is the result of another few minutes fiddling, this will work better, certainly.

      my $string; $string = 'lasdufaner%.alsdfi,' x 100; # $string = 'freddy\'s wife wilma, ' x 100; my $max = 100; if ( $string and length $string > $max ){ $string = substr( $string, 0, $max); my ($tmp) = $string =~ /(.+)\s.*?$/; # last space if possible $tmp or ($tmp) = $string =~ /(.+)\W.*?$/; # bust on last non-word $tmp and $string = $tmp; print $string }


      freddy's wife output:
      freddy's wife wilma, freddy's wife wilma, freddy's wife wilma, freddy's wife wilma, freddy's wife

      lasd... output
      lasdufaner%.alsdfi,lasdufaner%.alsdfi,lasdufaner%.alsdfi,lasdufaner%.alsdfi,lasdufaner%.alsdfi

      The point being, I suppose, that this sort of thing might be easily handled by a regular expression in most cases.

      Thanks for your comment though, it's always good to have a second set of eyes. :-)

      \s

        but a suggestion that a regex approach might make sense.

        That had already been done.

        this will work better, certainly.

        That's a lot of code. Does it do anything different than the following?

        print $string =~ /^(.{0,100})(?!\S)/;