Thanks. Both solutions work.

On Tue, Sep 23, 2008 at 9:19 PM, Clemens Bieg <[EMAIL PROTECTED]> wrote:

> Thanks.
>
> On Tue, Sep 23, 2008 at 8:25 PM, John W. Krahn <[EMAIL PROTECTED]> wrote:
>
>>  Clemens Bieg wrote:
>>
>>> Hello,
>>>
>>> I am trying to iterate over all the lines of a file, each of which starts
>>> with a line number, and isolate the line number and the corresponding
>>> string
>>> into one variable each, then have them printed into a separate file.
>>> Could
>>> someone give me a clue on how to assign the line number match to the
>>> variable so I can reuse it? Here is what I am trying (mainly by analogy
>>> with
>>> examples I found on the web and in books, but I am lacking the background
>>> knowledge). At this point, all I am trying is to see if I can get the
>>> line
>>> number into a variable, and print it out.
>>>
>>>
>>> use strict;
>>> use warnings;
>>> my ($qfn_in_extract) = "extract/extract.txt";
>>> my ($qfn_in_allnumbered) = "extract/allnumbered.txt";
>>> my $dir = mkdir("final");
>>>
>>> open(my $fh_in_extract, '<', $qfn_in_extract)
>>>   or die("Unable to read file \"extract.txt\": $!\n");
>>> open(my $fh_in_allnumbered, '<', $qfn_in_allnumbered)
>>>   or die("Unable to read file \"allnumbered.txt\": $!\n");
>>> open(my $fh_out_final, '>', "final/final.txt")
>>>   or die("Unable to create file \"final.txt\": $!\n");
>>>
>>> my $id = '';
>>>
>>> while (<$fh_in_extract>) {
>>>
>>>   if ($id = m/^\d+/) {
>>>   print $fh_out_final $id;
>>>   }
>>> }
>>>
>>> #   my $string = m/(?<=\d ).*?$/;
>>> #   print $fh_out_final $_;
>>>
>>>
>>> This only gives me the return status. I have been playing with and
>>> without
>>> the if statement, with and without the $id variable, with $_ instead of
>>> $id,
>>> and with =~ instead of =.  But I do not see the light... I would
>>> appreciate
>>> if someone could point me in the right direction.
>>>
>>
>> It looks like you need to capture the number using capturing parentheses:
>>
>> while ( <$fh_in_extract> ) {
>>
>>   if ( /^(\d+)/ ) {
>>      print $fh_out_final $1;
>>   }
>> }
>>
>>
>> perldoc perlretut
>> perldoc perlre
>>
>>
>>
>> John
>> --
>> Perl isn't a toolbox, but a small machine shop where you
>> can special-order certain sorts of tools at low cost and
>> in short order.                            -- Larry Wall
>>
>> --
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
>> http://learn.perl.org/
>>
>>
>>
>

Reply via email to