To count # characters in a line and make the line shorter

C

clearguy02

Hi experts,

In my script, I need to check if a total number of characters in a
line is more than 10 or not and it it is >10, I should the cut the
original line to only 10 characters.

Example:
=============
$orig_line = "Perl is a good language";
$total_char = length ($orig_line)";

$second_line;

if ($total_char > 10)
{
print "The original's size is $total_char, but it should be
$second_line";
}

One way I can think is to get the difference between the $total_char
and the limit value (10) and after the finding the difference number,
I can truncate those many characters from the original line and store
that modified line in $second_line variable.

Does some one suggest me a shorter way of doing it?

Thanks,
J
 
J

Jürgen Exner

In my script, I need to check if a total number of characters in a
line is more than 10 or not and it it is >10, I should the cut the
original line to only 10 characters.

Example:
=============

You are missing
use strict; use warnings;
$orig_line = "Perl is a good language";
[complicated, non-compilable code snipped]

This does smell like homework, but I'm bored today:

use warnings;
use strict;
my $orig_line = "Perl is a good language";
if (length $orig_line > 10){
print ('The original\'s size is ',
length $orig_line ,
' total_char, but it should be ',
substr($orig_line, 0, 10)
)
}


jue
 
J

John W. Krahn

Hi experts,

In my script, I need to check if a total number of characters in a
line is more than 10 or not and it it is >10, I should the cut the
original line to only 10 characters.

Example:
=============
$orig_line = "Perl is a good language";
$total_char = length ($orig_line)";

$second_line;

if ($total_char > 10)
{
print "The original's size is $total_char, but it should be
$second_line";
}

One way I can think is to get the difference between the $total_char
and the limit value (10) and after the finding the difference number,
I can truncate those many characters from the original line and store
that modified line in $second_line variable.

Does some one suggest me a shorter way of doing it?


my $new_line = substr $orig_line, 0, 10;




John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top