Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

sqldatasource dal, wwwxxxenden, convert raid5 to raid 10 mdadm, apache force chunked, nrao wwwxxx, xxxxxdup, procmail change subject header, wwwXxx not20, Wwwxxx.doks sas, linux raid resync after reboot

Links

XODOX
Impressum

#1: Help: Print lines

Posted on 2008-04-24 09:12:38 by 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

Report this message

#2: Re: Help: Print lines

Posted on 2008-04-24 09:44:37 by 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 ( <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>

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

Report this message

#3: Re: Help: Print lines

Posted on 2008-04-24 10:05:48 by 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 ( <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. And if the content dose not just contain
2 lines, multi lines, what should I do?

Thank you again.

Amy

Report this message

#4: Re: Help: Print lines

Posted on 2008-04-24 10:54:25 by 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 ( <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

Report this message

#5: Re: Help: Print lines

Posted on 2008-04-24 12:43:03 by 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 ( <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.

It isn't "print scalar" it is "print X" where X is "scalar <DATA>"

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 = <DATA>) {
if ($record=~/CT2\n(.*)\n/s) { print $1 }
}

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





--
RGB

Report this message

#6: Re: Help: Print lines

Posted on 2008-04-24 12:52:24 by RedGrittyBrick

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

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

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



--
RGB

Report this message

#7: Re: Help: Print lines

Posted on 2008-04-24 15:44:58 by January Weiner

Amy Lee <openlinuxsource@gmail.com> wrote:
[snip]

Use bioperl to parse FASTA files :-)

j.

Report this message