|
Posted by Gunnar Hjalmarsson on April 24, 2008, 4:54 am
Please log in for more thread options Amy Lee wrote:
> On Thu, 24 Apr 2008 09:44:37 +0200, Gunnar Hjalmarsson wrote:
>> Amy Lee wrote:
>>> My file is like is
>>>
>>>> CT1
>>> XY0002658-96
>>> 0000222541
>>>> CT2
>>> XY0002688-55
>>> 0000254147
>>>> CT5
>>> ZZ0004854-00
>>> 0000475568
>>> ...........
>>>
>>> And I hope when some conditions match 'CT1', then can print its contents
'XY0002658-96
>>> 0000222541', if match 'CT2' print 'XY0002688-55 0000254147'.
>> C:\home>type test.pl
>> while ( <DATA> ) {
>> if ( /CT2/ ) {
>> print scalar <DATA>;
>> print scalar <DATA>;
>> }
>> }
>>
>> __DATA__
>> >CT1
>> XY0002658-96
>> 0000222541
>> >CT2
>> XY0002688-55
>> 0000254147
>> >CT5
>> ZZ0004854-00
>> 0000475568
>>
>> C:\home>test.pl
>> XY0002688-55
>> 0000254147
>>
>> C:\home>
>
> Thank you very much. But I just have Learning Perl this book and I didn't
> find out what "print scalar" is.
Assuming you know what print() is, please check out
perldoc -f scalar
> And if the content dose not just contain
> 2 lines, multi lines, what should I do?
Then the above approach isn't sufficient. Something like this might do:
while ( <DATA> ) {
if ( /CT2/ ) {
while ( <DATA> ) {
last if /^>/;
print;
}
}
}
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
|