lina wrote:
> On Sun, Mar 11, 2012 at 11:15 PM, Ron Bergin <r...@i.frys.com> wrote:
>> lina wrote:
>>> On Sun, Mar 11, 2012 at 10:45 PM, lina <lina.lastn...@gmail.com> wrote:
>>>
>>> What I have come up so far :
>>>
>>> #!/usr/bin/env perl
>>>
>>> use strict;
>>> use warnings;
>>>
>>> my $filename = "try.txt";
>>>
>>> open my $fh, '<', $filename or die "Couldn't read $filename";
>>
>> You should include the reason it failed in the die statement.
>>
>> open my $fh, '<', $filename or die "Couldn't read $filename <$!>";
>>>
>>>
>>>
>>> while (my $line = <$fh>){
>>>       if ($line =~ /^A$/){
>>
>> Use the range operator to delimit the section you want to extract.
>> You can read about in 'perldoc perlop'
>>
>> if ($line =~ /^A$/ .. $line =~ /^C$/) {
>>
>>>               ## Here I don't know how to proceed further
>>>               print $line;
>>
>> You'll want to skip over the 'A' and 'C' lines before printing the line.
>> I'll leave that to you.
> Thank you Ron, I never known how amazing $line =~ /A$/ .. $line =~ /C$/
> can do.
> I don't know how to skip the 'A' and 'C' lines, the only way I could
> think of using grep -v in the output file.
> check the "next", but not so sure how to do this,
>
> #!/usr/bin/env perl
>
> use strict;
> use warnings;
>
> my $filename = $ARGV[0];
> my $outfile = $ARGV[1];
>
> open my $fh, '<', $filename or die "Couldn't open '$filename' for reading:
> $!";
>
> open my $OUT, '>>', $outfile or die "Couldn't open '$outfile' for writing:
> $!";
>
>
> while (my $line = <$fh>){
>
>       if ($line =~ /C$/ .. $line =~ /Si$/){
>
>               print $OUT $line;
>       }
>       #if ($line =~ /^C/..$line =~ /^A$/){
>       #       print $line;
>       #}
> }
>
>
> $ perl calculate_COM.pl com.txt A.txt
>
> $ more com.txt
> Si
> 5 3 1 0 0 0 0 1 0 0 0 0 0
> 5 5 1 0 0 0 0 5 0 0 0 0 0
> 3 6 1 0 0 0 0 6 0 0 0 0 0
> C
> 8 3 1 0 0 0 0 1 0 0 0 0 0
> 3 5 1 0 0 0 0 4 0 0 0 0 0
> 1 6 1 0 0 0 0 6 0 0 0 0 0
> C
> 1 3 1 0 0 0 0 2 0 0 0 0 0
> 5 5 1 0 0 0 0 5 0 0 0 0 0
> 3 6 1 0 0 0 0 6 0 0 0 0 0
> Si
> 1 3 1 0 0 0 0 1 0 0 0 0 0
> 2 5 1 0 0 0 0 5 0 0 0 0 0
> 3 6 1 0 0 0 0 6 0 0 0 0 0
>
> I changed the examples to make is simple and readable.
>
> Thanks with best regards,
>
>>
>>>       }
>>> }
>>>
>>> Thanks
>>>
>>> --
> --

Which section(s) in that last example do you want to extract?

while (my $line = <$fh>) {
    if ($line =~ /^C$/ .. $line =~ /^Si$/) {
        print {$OUT} $line unless $line =~ /^(C|Si)$/;
    }
}

or flip it to get the other 2 sections

while (my $line = <$fh>) {
    if ($line =~ /^Si$/ .. $line =~ /^C$/) {
        print {$OUT} $line unless $line =~ /^(C|Si)$/;
    }
}


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to