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

I would like to take a string with 10+ digits and pull out just the first 10. This won't matter if there are 10, 12, or even 20. I just want the first 10. Is there some sort of regexp matching function I could use? Thanks.
  • Comment on Pulling first 10 characters out of string?

Replies are listed 'Best First'.
Re: Pulling first 10 characters out of string?
by sgifford (Prior) on Dec 13, 2004 at 15:58 UTC
    Read the documentation for the substr function.
      Thanks for all the help. I think I was being a little lazy. Thanks again everyone!
Re: Pulling first 10 characters out of string?
by exussum0 (Vicar) on Dec 13, 2004 at 16:03 UTC
    Better to use substr, but this caught my eye...
    $x = "abcdefghijklmop"; print $x =~/^(.{10})/;

    ----
    Give me strength for today.. I will not talk it away..
    Just for a moment.. It will burn through the clouds.. and shine down on me.

      The op did say "digits", so what about
      print $x =~/^(\d{10})/;
        Definitely have a point. Guess it depends on where the data validation goes on.

        ----
        Give me strength for today.. I will not talk it away..
        Just for a moment.. It will burn through the clouds.. and shine down on me.

        Neither "$x =~/^(.{10})/;" nor "$x =~/^(\d{10})/;" seem to work. When I print them out, I still have 11 characters. A little more info. The variable has already been set, to a string with 10 or more digits. I want to reset the value to just the first 10 digits. This is what I tried: $x = "12345678901"; $x =~ /^(\d{10})/; ( I also tried $x =~ /^(.{10})/; ) print "$x\n"; This still prints "12345678901", instead of "12345678900". I'll look at the substr doc. Thanks!
Re: Pulling first 10 characters out of string?
by hmerrill (Friar) on Dec 13, 2004 at 16:07 UTC
    And here's a snippet from 'perldoc perl':
    perlre Perl regular expressions, the rest of the story perlreref Perl regular expressions quick reference
    So, to read the perldocs on perlre, just do
    perldoc perlre
    at a command prompt.
Re: Pulling first 10 characters out of string?
by nedals (Deacon) on Dec 13, 2004 at 21:48 UTC
    use strict; my $x = "12345678901234"; # This evalutes the regex and prints the result print $x =~ /^(\d{10})/; print "\n"; $x = "12345678901234"; # This evaluates the regex but the result is not assigned anywhere. So + you get back $x $x =~ /^(\d{10})/; print "$x\n"; $x = "12345678901234"; # This evaluates the regex and assigns the result to $y my ($y) = $x =~ /^(\d{10})/; print "$y\n"; $x = "12345678901234"; # This perform a substitution, returning the result in $x $x =~ s/^(\d{10})\d*$/$1/; print "$x\n";
    Update:
    Did not see post by Eimi Metamorphoumai (below my depth setting)
    This is somewhat of a repeat.
Re: Pulling first 10 characters out of string?
by Anonymous Monk on Dec 13, 2004 at 16:16 UTC
    if you really want to burn some cpu cycles use:
    $x="1234567890asdfg"; while (length ($x) > 10) {chop($x)}; print $x;
    but otherwise use substr()