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

Discussion in 'Perl Misc' started by clearguy02@yahoo.com, May 9, 2009.

  1. Guest

    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
     
    , May 9, 2009
    #1
    1. Advertising

  2. wrote:
    >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ürgen Exner, May 9, 2009
    #2
    1. Advertising

  3. wrote:
    > 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
    --
    Those people who think they know everything are a great
    annoyance to those of us who do. -- Isaac Asimov
     
    John W. Krahn, May 9, 2009
    #3
  4. Frank Seitz Guest

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


    $second_line = substr $orig_line,0,10;

    Frank
    --
    Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
    Anwendungen für Ihr Internet und Intranet
    Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel
     
    Frank Seitz, May 9, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hugo
    Replies:
    10
    Views:
    1,399
    Matt Humphrey
    Oct 18, 2004
  2. Lowell Kirsh

    how can I make this script shorter?

    Lowell Kirsh, Feb 22, 2005, in forum: Python
    Replies:
    7
    Views:
    290
    Christos TZOTZIOY Georgiou
    Feb 25, 2005
  3. Replies:
    6
    Views:
    490
    7stud
    Apr 7, 2008
  4. Kris Thomsen
    Replies:
    2
    Views:
    107
    Kris Thomsen
    Apr 15, 2009
  5. Treetop

    make a time script shorter

    Treetop, Oct 3, 2003, in forum: Javascript
    Replies:
    1
    Views:
    83
    Dr John Stockton
    Oct 3, 2003
Loading...

Share This Page