a length of variable question

Discussion in 'Perl Misc' started by justme, Nov 22, 2004.

  1. justme

    justme Guest

    hi

    in my script i will process from a database and assign a value to a
    variable $var1
    The value assigned to $var1 is a number. eg 1, 2 or 3. But it is in
    "string" format. There will be spaces in front as well as behind it.

    eg

    $var1 = xxxxxxx2xxx
    or
    $var1 = xxx3xxxxxxxxxxx

    where x = spaces.

    I want to remove these spaces and just get the number. so i tried to
    use

    $var1 =~ s/(.*)(\d)(.*)/\2/ ;

    Then when i print the length of $var1, it says length is 2. Is there
    anything wrong with my regexp?
    thanks for help
    justme, Nov 22, 2004
    #1
    1. Advertising

  2. justme

    Tony Curtis Guest

    >> On 21 Nov 2004 18:59:30 -0800,
    >> (justme) said:


    > hi in my script i will process from a database and assign a
    > value to a variable $var1 The value assigned to $var1 is a
    > number. eg 1, 2 or 3. But it is in "string" format. There
    > will be spaces in front as well as behind it.


    would just casting it into an int work?

    $m = ' 4 ';
    $n = int $m;
    print "$n\n";

    4
    Tony Curtis, Nov 22, 2004
    #2
    1. Advertising

  3. (justme) wrote in
    news::

    > in my script i will process from a database and assign a value to a
    > variable $var1
    > The value assigned to $var1 is a number. eg 1, 2 or 3. But it is in
    > "string" format. There will be spaces in front as well as behind it.
    >
    > eg
    >
    > $var1 = xxxxxxx2xxx
    > or
    > $var1 = xxx3xxxxxxxxxxx
    >
    > where x = spaces.
    >
    > I want to remove these spaces and just get the number. so i tried to
    > use
    >
    > $var1 =~ s/(.*)(\d)(.*)/\2/ ;


    Please read the posting guidelines posted here frequently.

    You seem not to be using strict or warnings.

    C:\Home> cat t6.pl
    use strict;
    use warnings;

    my $m = ' 4 ';
    $m =~ s/(.*)(\d)(.*)/\2/ ;
    print $m, "\n", length $m, "\n";

    C:\Home> t6
    \2 better written as $2 at C:\Home\asu1\t6.pl line 5.
    4
    1

    > Then when i print the length of $var1, it says length is 2. Is there
    > anything wrong with my regexp?


    If you are not going to use the leading and trailing whitespace, there is
    no need to capture it.

    C:\Home> cat t6.pl
    use strict;
    use warnings;

    my $m = ' 4 ';
    $m =~ s/\s*(\d)\s*/$1/ ;
    print $m, "\n", length $m, "\n";

    C:\Home> t6
    4
    1

    But then, if all you want is to remove space characters, there is no need
    to use s/// anyway:

    C:\Home> cat t6.pl
    use strict;
    use warnings;

    my $m = ' 4 ';
    $m =~ tr/ //d;
    print $m, "\n", length $m, "\n";

    C:\Home> t6
    4
    1


    --
    A. Sinan Unur
    d
    (remove '.invalid' and reverse each component for email address)
    A. Sinan Unur, Nov 22, 2004
    #3
  4. justme wrote:
    > in my script i will process from a database and assign a value to a
    > variable $var1
    > The value assigned to $var1 is a number. eg 1, 2 or 3. But it is in
    > "string" format. There will be spaces in front as well as behind it.
    >
    > eg
    >
    > $var1 = xxxxxxx2xxx
    > or
    > $var1 = xxx3xxxxxxxxxxx
    >
    > where x = spaces.
    >
    > I want to remove these spaces and just get the number. so i tried to
    > use
    >
    > $var1 =~ s/(.*)(\d)(.*)/\2/ ;
    >
    > Then when i print the length of $var1, it says length is 2. Is there
    > anything wrong with my regexp?


    1) The capturing parenteses around '.*' are redundant.

    2) If the length of $var1 is 2 after that operation, the string contains
    anything else but a digit, probably a newline. Is that what you want?

    3) The regex does not cover the possibility that the number consists of
    more than one digit. Is that intentional?

    4) If you enable warnings (why didn't you do so before posting?), Perl
    will tell you about another thing that isn't good.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Nov 22, 2004
    #4
  5. justme wrote:
    > The value assigned to $var1 is a number. eg 1, 2 or 3. But it is in
    > "string" format. There will be spaces in front as well as behind it.
    >
    > eg
    >
    > $var1 = xxxxxxx2xxx
    > or
    > $var1 = xxx3xxxxxxxxxxx
    >
    > where x = spaces.
    >
    > I want to remove these spaces and just get the number. so i tried to
    > use
    >
    > $var1 =~ s/(.*)(\d)(.*)/\2/ ;


    Way, way to complicated.
    As I wrote just yesterday in a different thread to cast a string to a
    numerical value you simply add 0.

    my $var1 = ' 3 ';
    print $var1 + 0;

    This will work as long as there are just blanks before the number.

    jue
    Jürgen Exner, Nov 22, 2004
    #5
  6. On Sun, 21 Nov 2004 21:05:20 -0600, Tony Curtis wrote:

    >>> On 21 Nov 2004 18:59:30 -0800,
    >>> (justme) said:

    >
    >> hi in my script i will process from a database and assign a
    >> value to a variable $var1 The value assigned to $var1 is a
    >> number. eg 1, 2 or 3. But it is in "string" format. There
    >> will be spaces in front as well as behind it.

    >
    > would just casting it into an int work?
    >
    > $m = ' 4 ';
    > $n = int $m;
    > print "$n\n";
    >
    > 4


    It would ... if Perl were another language :)

    scalars (very simply those variables represented with a '$' in front of
    the variable name) are type independent in Perl. Other languages (like
    C++ and Java) *required* you to associate a variable with a type - Perl
    doesn't.

    `perldoc -q scalar` might help, as well as `perldoc perldata`

    HTH

    Jim
    James Willmore, Nov 22, 2004
    #6
  7. justme

    Paul Lalli Guest

    "James Willmore" <> wrote in message
    news:p...
    > On Sun, 21 Nov 2004 21:05:20 -0600, Tony Curtis wrote:
    >
    > >>> On 21 Nov 2004 18:59:30 -0800,
    > >>> (justme) said:

    > >
    > >> hi in my script i will process from a database and assign a
    > >> value to a variable $var1 The value assigned to $var1 is a
    > >> number. eg 1, 2 or 3. But it is in "string" format. There
    > >> will be spaces in front as well as behind it.

    > >
    > > would just casting it into an int work?
    > >
    > > $m = ' 4 ';
    > > $n = int $m;
    > > print "$n\n";
    > >
    > > 4

    >
    > It would ... if Perl were another language :)


    This language will do just fine.

    > scalars (very simply those variables represented with a '$' in front

    of
    > the variable name) are type independent in Perl. Other languages

    (like
    > C++ and Java) *required* you to associate a variable with a type -

    Perl
    > doesn't.


    All of which is wholly irrelevant to the discussion at hand.

    Perl doesn't have strict 'types', that's true. But it does have
    contexts. Specifically, it has both a string and an numeric context.
    When a variable containing a string is used in a numeric context - such
    as that provided by the int() function - perl scans the variable
    character by character to determine what number the string represents.
    Leading whitespace is ignored in this scan.

    > `perldoc -q scalar` might help, as well as `perldoc perldata`


    perldoc -f int might help you, as would simply running the example
    program above.

    Hope that clarifies,
    Paul Lalli
    Paul Lalli, Nov 22, 2004
    #7
  8. James Willmore <> wrote:
    > On Sun, 21 Nov 2004 21:05:20 -0600, Tony Curtis wrote:
    >>>> On 21 Nov 2004 18:59:30 -0800,
    >>>> (justme) said:

    >>


    >>> There
    >>> will be spaces in front as well as behind it.

    >>
    >> would just casting it into an int work?
    >>
    >> $m = ' 4 ';
    >> $n = int $m;
    >> print "$n\n";
    >>
    >> 4

    >
    > It would ...



    It does!


    > if Perl were another language :)



    Tony's code looked just like Perl to me. :)


    > scalars (very simply those variables represented with a '$' in front of
    > the variable name) are type independent in Perl.



    Right, but what was wanted by the OP was to remove leading and trailing
    whitespace, and Tony's code does just that (but it is not "casting"
    at all).

    It works because of the rules Perl uses when converting a
    string into a number:

    1) ignore leading whitespace

    2) ignore anything after the 1st char that cannot be part of a number.

    Rule 1 removes the leading, Rule 2 removes the trailing.

    Of course, the $n temporary variable is not needed:

    $m = int $m;
    or
    $m = 0 + $m;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Nov 22, 2004
    #8
  9. Also sprach Tad McClellan:

    > It works because of the rules Perl uses when converting a
    > string into a number:
    >
    > 1) ignore leading whitespace
    >
    > 2) ignore anything after the 1st char that cannot be part of a number.
    >
    > Rule 1 removes the leading, Rule 2 removes the trailing.
    >
    > Of course, the $n temporary variable is not needed:
    >
    > $m = int $m;
    > or
    > $m = 0 + $m;


    And yet, I'd drop the first of the anove two alternatives. int() is
    really used for dropping the decimal part of a floating point number.
    It's not its primary purpose to convert a string to a number as it will
    only convert it to an integer. This starts to matter when $m is
    something like

    $m = ' 1.5 ';

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
    Tassilo v. Parseval, Nov 22, 2004
    #9
  10. justme

    Tony Curtis Guest

    >> On Mon, 22 Nov 2004 10:46:11 -0600,
    >> Tad McClellan <> said:


    > Right, but what was wanted by the OP was to remove leading
    > and trailing whitespace, and Tony's code does just that (but
    > it is not "casting" at all).


    Yeah, years of C wrecks yer brain :)

    > Of course, the $n temporary variable is not needed:
    > $m = int $m; or $m = 0 + $m;


    I usually include more intermediate variables in examples than
    I might ordinarily use in "real" code.

    t
    Tony Curtis, Nov 22, 2004
    #10
  11. On Mon, 22 Nov 2004 10:46:11 -0600, Tad McClellan wrote:

    > James Willmore <> wrote:
    >> On Sun, 21 Nov 2004 21:05:20 -0600, Tony Curtis wrote:
    >>>>> On 21 Nov 2004 18:59:30 -0800,
    >>>>> (justme) said:


    >>>> There will be spaces in front as well as behind it. would just
    >>>> casting it into an int work?


    >>> $m = ' 4 ';
    >>> $n = int $m;
    >>> print "$n\n";


    >>> 4


    >> It would ...


    > It does!


    >> if Perl were another language :)


    > Tony's code looked just like Perl to me. :)


    Didn't know 'int' was in Perl :) My bad.

    Thanks for the clarification (sniped for brevity).
    James Willmore, Nov 23, 2004
    #11
  12. justme

    justme Guest

    (justme) wrote in message news:<>...
    > hi
    >
    > in my script i will process from a database and assign a value to a
    > variable $var1
    > The value assigned to $var1 is a number. eg 1, 2 or 3. But it is in
    > "string" format. There will be spaces in front as well as behind it.
    >
    > eg
    >
    > $var1 = xxxxxxx2xxx
    > or
    > $var1 = xxx3xxxxxxxxxxx
    >
    > where x = spaces.
    >
    > I want to remove these spaces and just get the number. so i tried to
    > use
    >
    > $var1 =~ s/(.*)(\d)(.*)/\2/ ;
    >
    > Then when i print the length of $var1, it says length is 2. Is there
    > anything wrong with my regexp?
    > thanks for help


    thanks guys for your help.
    my original intention was to get rid of everything before the digit
    and after.
    I do not know what other embedded characters would be infront of the
    digit (which i can't 'see') besides the spaces and newlines, so my
    intention was to get rid of everything before the digit as well as
    after it. that's why i use (.*). Since its redundant as suggested in
    some posts...i would refine the expression. Thanks.
    Anyway, i found the problem, its a newline. But why doesn't (.*) get
    rid of it?
    hmm... I did not use warnings as my perl version is very old. :)
    Thanks again for the help
    justme, Nov 23, 2004
    #12
  13. justme wrote:
    [RE question]
    > Anyway, i found the problem, its a newline. But why doesn't (.*) get
    > rid of it?


    Which part of "perldoc perlre" don't you understand?

    <quote>
    In particular the following metacharacters have their standard
    *egrep*-ish meanings:
    [...]
    . Match any character (except newline)
    </quote>

    > hmm... I did not use warnings as my perl version is very old. :)


    Maybe time to upgrade?

    jue
    Jürgen Exner, Nov 23, 2004
    #13
    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. Mitchua
    Replies:
    5
    Views:
    2,705
    Eric J. Roode
    Jul 17, 2003
  2. =?Utf-8?B?SG96aQ==?=
    Replies:
    1
    Views:
    6,928
    Ken Cox [Microsoft MVP]
    Jun 2, 2004
  3. Sam
    Replies:
    3
    Views:
    14,066
    Karl Seguin
    Feb 17, 2005
  4. Replies:
    2
    Views:
    5,902
  5. Dan Manes
    Replies:
    1
    Views:
    715
    David Browne
    Apr 23, 2006
Loading...

Share This Page