Help: Print lines

Help: Print lines

am 24.04.2008 09:12:38 von Amy Lee

Hello,

I'm a newbie in Perl. And I face a problem when I process the data from a
file. 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'. However, when I use
if /CT1/
{
print;
}
just print the label, what if I hope print contents, what should I notice?

Thank you very much~

Regards,

Amy Lee

Re: Help: Print lines

am 24.04.2008 09:44:37 von Gunnar Hjalmarsson

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 ( ) {
if ( /CT2/ ) {
print scalar ;
print scalar ;
}
}

__DATA__
>CT1
XY0002658-96
0000222541
>CT2
XY0002688-55
0000254147
>CT5
ZZ0004854-00
0000475568

C:\home>test.pl
XY0002688-55
0000254147

C:\home>

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Re: Help: Print lines

am 24.04.2008 10:05:48 von Amy Lee

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 ( ) {
> if ( /CT2/ ) {
> print scalar ;
> print scalar ;
> }
> }
>
> __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. And if the content dose not just contain
2 lines, multi lines, what should I do?

Thank you again.

Amy

Re: Help: Print lines

am 24.04.2008 10:54:25 von Gunnar Hjalmarsson

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 ( ) {
>> if ( /CT2/ ) {
>> print scalar ;
>> print scalar ;
>> }
>> }
>>
>> __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 ( ) {
if ( /CT2/ ) {
while ( ) {
last if /^>/;
print;
}
}
}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Re: Help: Print lines

am 24.04.2008 12:43:03 von RedGrittyBrick

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 ( ) {
>> if ( /CT2/ ) {
>> print scalar ;
>> print scalar ;
>> }
>> }
>>
>> __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.

It isn't "print scalar" it is "print X" where X is "scalar "

perldoc -f scalar

> And if the content dose not just contain
> 2 lines, multi lines, what should I do?

perldoc -q paragraph

------------------ 8< ------------------
#!/usr/bin/perl
#
use strict;
use warnings;

$/ = "\n >";
while (my $record = ) {
if ($record=~/CT2\n(.*)\n/s) { print $1 }
}

__DATA__
>CT1
XY0002658-96
0000222541
>CT2
XY0002688-55
0000254147
>CT5
ZZ0004854-00
0000475568
------------------ 8< ------------------





--
RGB

Re: Help: Print lines

am 24.04.2008 12:52:24 von RedGrittyBrick

RedGrittyBrick wrote:
> #!/usr/bin/perl
> #
> use strict;
> use warnings;
>
> $/ = "\n >";
> while (my $record = ) {
> if ($record=~/CT2\n(.*)\n/s) { print $1 }
> }

Or
$/ = "\n >";
while () {
print $1 if /CT5\n(.*)\n/s;
}

>
> __DATA__
> >CT1
> XY0002658-96
> 0000222541
> >CT2
> XY0002688-55
> 0000254147
3333333333
4444444444
> >CT5
> ZZ0004854-00
> 0000475568



--
RGB

Re: Help: Print lines

am 24.04.2008 15:44:58 von January Weiner

Amy Lee wrote:
[snip]

Use bioperl to parse FASTA files :-)

j.