Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

The first two number

by Anonymous Monk
on Feb 18, 2005 at 16:40 UTC ( [id://432397]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

For example 77373737377837, how do I take the first two number that is 77.

Such as $no = 77373737377837 I want $newno = 77.

Replies are listed 'Best First'.
Re: The first two number
by gellyfish (Monsignor) on Feb 18, 2005 at 16:46 UTC

    A selection of more or less useful ways of doing it.

    $newno = substr($no,0,2); $no =~ /^(\d\d)/ && $newno = $1; $newno = join '', unpack("AA", $no); $newno = join '', (split //, $no)[0,1]; $newno = int($no / (10 ** (length($no) -2))); etc ...

    /J\

Re: The first two number
by JediWizard (Deacon) on Feb 18, 2005 at 16:43 UTC

    If You just want the first two characters in a string (numbers are sort of strings in perl, the distinction is kind of fuzzy), you can use this:

    my $newno = substr($no, 0, 2);

    See: substr

    I hope this is what you wanted. If not let me know, and I'll try to help.

    May the Force be with you
Re: The first two number
by gopalr (Priest) on Feb 19, 2005 at 04:25 UTC

    Use ^ to start from beginning of the line.

    $no = '77373737377837'; $newno = $1 if $no=~m#^([0-9]{2})#; print "\n$newno";
Re: The first two number
by DamnDirtyApe (Curate) on Feb 18, 2005 at 16:44 UTC
    perldoc -f substr

    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
Re: The first two number
by hubb0r (Pilgrim) on Feb 19, 2005 at 04:59 UTC
    If as stated earlier you are looking for the first match that is a doubled number, I would do it like this:
    #!/usr/local/bin/perl -w use strict; my $var = '77373737377837'; my @new_var = $var =~ /((\d)$1)/; my $var2 = join('', @new_var);

    Please next time try to phrase your question more clearly.
      That's wrong. If you run the program, and ignore the warnings it issues, you'll see that $var2 ends up being 77. But it's not the first two numbers - it's the first number repeated twice. (Test it by setting $var to '7').

      The problem is your incorrect usage of $1, which is undefined (Correct would be \1). Since $1 is undefined, the regex becomes /((\d))/, putting the first number in both $1 and $2.

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://432397]
Approved by gellyfish
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-26 03:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found