#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Location
    Dallas
    Posts
    50
    Rep Power
    14

    Finding length of a variable in perl


    Hi,

    I am getting the value of $var by: $var = substr($addr,0,1);

    And depending on length of $var, I do some processing:

    if ($#var == 1) {
    ....................
    } elsif ($#var == 2) {
    ....................
    }

    But, I never go into this if condition even if $var = 7.

    Can someone help me with this. Thanks.

    -Yogesh.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2004
    Location
    Middle Europa
    Posts
    1,198
    Rep Power
    17
    yogi27
    - may i kindly ask you for a little more precision, what do you want?
    - read your code

    i am not a perl specialist, i just use use it
    the statement: $var = substr($addr,0,1);
    IMO, assign to $var the value of $addr starting by pos 0 ending by pos 1
    so if $addr is not empty, $var will always be the first char of $addr w/ length == 1

    perl sure knows a function (maybe) length() to compute a string length
    also: $mylen = length($var);

    what are you checking here: if ($#var == 1)
    NO SENSE for me.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Location
    Dallas
    Posts
    50
    Rep Power
    14
    This is what I want:

    If the length of the variable is 1, I'll append it by 2 zeros. If the length of the variable is 2, I'll append it by 1 zero. If the length of the variable is 3, I'll append it by 0 zeros.

    I want to get the value in 3 digits.

    Thanks,
    Yogesh.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2004
    Location
    Middle Europa
    Posts
    1,198
    Rep Power
    17
    you are not clear, whats means
    >>> If the length of the variable is 1, I'll append it by 2 zeros....
    a) var=a; do you want: a00(this appends) or 00a(this inserts) ?
    b) is var an int or char ?
    c) man pages: printf

IMN logo majestic logo threadwatch logo seochat tools logo