extracting numbers from a string

extracting numbers from a string

Post by Matthew Staplet » Sun, 23 Mar 2003 10:32:07



Hello,

I am learning Perl and having some fun with it.  In a script I am writing
I am wanting to read a line of a file and then extract a digit from the
beginning of the line.  A typical line looks something like this:

100. text text text

So I am using a file handle to open the file and read the lines on by one
with a while loop.  

while (<TXT>){}

That part is pretty straight forward. Each line is then given to $_

But how do I get just the digit at the beginning of the line out of $_ ?
I thought about adding zero to $_ and then passing this to another
variable but this does not seem to work.

Can someone help me?

Thanks,

Matthew Stapleton

#############
http://webpages.marshall.edu/~staple12

 
 
 

extracting numbers from a string

Post by b.. » Sun, 23 Mar 2003 17:36:06



Quote:> Hello,

> I am learning Perl and having some fun with it.  In a script I am writing
> I am wanting to read a line of a file and then extract a digit from the
> beginning of the line.  A typical line looks something like this:

> 100. text text text

> So I am using a file handle to open the file and read the lines on by one
> with a while loop.  

> while (<TXT>){}

> That part is pretty straight forward. Each line is then given to $_

> But how do I get just the digit at the beginning of the line out of $_ ?
> I thought about adding zero to $_ and then passing this to another
> variable but this does not seem to work.

> Can someone help me?

> Thanks,

> Matthew Stapleton

Welcome to Perl, Matthew.

You can (and in this case probably should)
use a regular expression; within your 'loop',
with your line in $_, try:

while (<TXT>){
 chomp;  # strips NL char at end of line
 next unless $_; # skips if empty line
 my ($nbr) = /^(\d)/;
 next unless $nbr; # line did not begin w/digit
 print "$nbr\n"; # show result

Quote:}

The meat of the above is the regex:

 /^(\d)/;

Where:
 / is the regex delimiter
 ^ says match at start of variable, $_
 ( 'capture' until )
 \d any digit 0-9
 ) end of 'capture'
 / end of regex

The (...) capture feature allows you to capture
matched parts of a regex; by default, the first
captured is put into special variable $1, the
second into $2, etc; if you call the regex in
list context (assigning the result to a list
or array), the captures are put into that list/
array. That's why I wrote ($nbr) above - list
context.

Now, if the first character of the line is not
a digit 0-9, nothing will be captured and $nbr
will be undefined.

Otherwise, $nbr contains the first digit of the
line.

If you would rather capture the entire number
that starts the line, use:

 my ($nbr) = /^(\d+)/;

\d+ says one or more (+) digits.

Study your regexs!  :)

Aloha => Beau;

PS: untested - you have to find my typos...

 
 
 

extracting numbers from a string

Post by Yarg » Sun, 23 Mar 2003 17:51:43


do you mean that you want the 100 in a variable?
If so, here is one way:
while <TXT>{
        # Splitting the line by white spaces.
        my $Number = (split /\s+/, $_)[0];

        # Deleting the dot at the end.

        print "$Number\n";

Quote:}

HTH,
Yargo.
-----Original Message-----

Sent: Saturday, March 22, 2003 3:32 AM

Subject: extracting numbers from a string

Hello,

I am learning Perl and having some fun with it.  In a script I am writing
I am wanting to read a line of a file and then extract a digit from the
beginning of the line.  A typical line looks something like this:

100. text text text

So I am using a file handle to open the file and read the lines on by one
with a while loop.  

while (<TXT>){}

That part is pretty straight forward. Each line is then given to $_

But how do I get just the digit at the beginning of the line out of $_ ?
I thought about adding zero to $_ and then passing this to another
variable but this does not seem to work.

Can someone help me?

Thanks,

Matthew Stapleton

#############
http://webpages.marshall.edu/~staple12

--



 
 
 

extracting numbers from a string

Post by Steve Grazzi » Sun, 23 Mar 2003 11:48:45



> [...] I am wanting to read a line of a file and then
> extract a digit from the beginning of the line.  A
> typical line looks something like this:

> 100. text text text

> So I am using a file handle to open the file and read
> the lines on by one with a while loop.  

> while (<TXT>){}

> That part is pretty straight forward. Each line is then
> given to $_

> But how do I get just the digit at the beginning of the
> line out of $_ ?  I thought about adding zero to $_ and
> then passing this to another variable but this does not
> seem to work.

Adding zero should work (with a warning)

    my $number = $_ + 0;

But usually you'll use split() or a regex to extract
the digits.

  while (<TXT>) {
    next unless /^(\d+)/;
    my $number = $1;
    ...
  }

Good place to start reading:

  $ perldoc perlrequick

--
Steve