Reputation: 669
I'm trying to truncate a string in a select input option using perl if it is longer than a set value, though i can't get it to work correctly.
my $value = defined $option->{value} ? $option->{value} : '';
my $maxValueLength = 50;
if ($value.length > $maxValueLength) {
$value = substr $value, 0, $maxValueLength + '...';
}
Upvotes: 1
Views: 263
Reputation: 66891
Another option is regex
$string =~ s/.{$maxLength}\K.*/.../;
It matches any character (.
) given number of times ({N}
, here $maxLength
), what is the first $maxLength
characters in $string
; then \K
makes it "forget" all previous matches so those won't get replaced later. The rest of the string that is matched is then replaced by ...
See Lookaround assertions in perlre for \K
.
This does start the regex engine for a simple task but it doesn't need any conditionals -- if the string is shorter than the maximum length the regex won't match and nothing happens.
Upvotes: 4
Reputation: 54333
Your code has several syntax errors. Turn on use strict
and use warnings
if you don't have it, and then read the error messages it tells you about. This is a bit tricky because of Perl's very complex syntax (see also Damian Conway's keynote from the 2020 Perl and Raku Conference), but it boils down to these:
I've used the following adaption of your code to produce these
use strict;
use warnings;
my $value = '1234567890' x 10;
my $maxValueLength = 50;
if ( $value.length > $maxValueLength ) {
$value = substr $value, 0, $maxValueLength + '...';
}
print $value;
Now let's see what they mean.
The .
operator in Perl is a concatenation. You cannot use it to call methods, and length
is not a method on a string. Perl thinks you are using the built-in length
(a function, not a method) without an argument, which makes it default to $_
. Most built-ins do this, to make one-liners shorter. But $_
is not defined. Now the .
tries to concatenate the length of undef
to $value
. And using undef
in a string operation leads to this warning.
The correct way of doing this is length $value
(or with parentheses if you prefer them, length($value)
).
The +
operator is not concatenation (we just learned that the .
is). It's a numerical addition. Perl is pretty good at converting between strings and numbers as there aren't really any types, so saying 1 + "5"
would give you 6
without problems, but it cannot do that for a couple of dots in a string. Hence it complains about a non-number value in an addition.
You want the substring with a given length, and then you want to attach the three dots. Because of associativity (or stickyness) of operators you will need to use parentheses ()
for your substr
call.
$value = substr($value, 0, $maxValueLength) . '...';
Upvotes: 3
Reputation: 3222
To find a length of the string use length(STRING)
Here is the code snippet how you can modify the script.
#!/usr/bin/perl
use strict;
use warnings;
use feature qw(say);
my $string = "abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz";
say "length of original string is:".length($string);
my $value = defined $string ? $string : '';
my $maxValueLength = 50;
if (length($value) > $maxValueLength) {
$value = substr $value, 0, $maxValueLength;
say "value:$value";
say "value's length:".length($value);
}
Output:
length of original string is:80
value:abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvw
value's length:50
Upvotes: 0