|
From: Amy Lee on 24 Apr 2008 03:12 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
From: Gunnar Hjalmarsson on 24 Apr 2008 03:44 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
From: Amy Lee on 24 Apr 2008 04:05 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
From: Gunnar Hjalmarsson on 24 Apr 2008 04:54 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
From: RedGrittyBrick on 24 Apr 2008 06:43
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 |