### Re^2: Pulling first 10 characters out of string?

by hmerrill (Friar)
 on Dec 13, 2004 at 16:08 UTC Need Help??

The op did say "digits", so what about
```   print \$x =~/^(\d{10})/;

Replies are listed 'Best First'.
Re^3: Pulling first 10 characters out of string?
by exussum0 (Vicar) on Dec 13, 2004 at 16:13 UTC
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.

Re^3: Pulling first 10 characters out of string?
by Anonymous Monk on Dec 13, 2004 at 20:29 UTC
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!
What's happening is that "\$x =~ /^(.{10)/" is matching the first ten characters, and copying them into \$1 for future use. If you want to replace \$x, you want one of
```\$x =~ s/^(.{10}).*/\$1/;
(\$x) = \$x =~ /^(.{10})/;
\$x = substr(\$x, 0, 10);
susbstr(\$x, 10) = '';
Any of them work, so use whichever style you like most (personally, I think I prefer the last one).
Then you want substr(\$x,10)=""; (though that will give an error if \$x isn't at least 10 characters).

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://414424]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-02-22 10:15 GMT
Voting Booth?
My favourite way to spend a leap day ...

Results (22 votes). Check out past polls.