On 21 Mar 2003 at 20:32, Matthew Stapleton wrote:

> 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
}

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...




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to