Click here to get back home

Help: Print lines

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Help: Print lines Amy Lee 04-24-2008
---> Re: Help: Print lines Gunnar Hjalmars...04-24-2008
Posted by Amy Lee on April 24, 2008, 3:12 am
Please log in for more thread options
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

Posted by Gunnar Hjalmarsson on April 24, 2008, 3:44 am
Please log in for more thread options
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

Posted by Amy Lee on April 24, 2008, 4:05 am
Please log in for more thread options
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

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

Posted by RedGrittyBrick on April 24, 2008, 6:43 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.

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

Similar ThreadsPosted
FAQ 6.0: Why do I get weird spaces when I print an array of lines? December 18, 2004, 6:03 am
FAQ 6.0: Why do I get weird spaces when I print an array of lines? January 1, 2005, 6:03 pm
FAQ 6.0 Why do I get weird spaces when I print an array of lines? February 25, 2005, 6:03 am
FAQ 5.38 Why do I get weird spaces when I print an array of lines? May 8, 2005, 5:03 am
FAQ 5.38 Why do I get weird spaces when I print an array of lines? July 23, 2005, 10:03 pm
FAQ 5.38 Why do I get weird spaces when I print an array of lines? October 9, 2005, 10:03 am
FAQ 5.38 Why do I get weird spaces when I print an array of lines? December 15, 2005, 5:03 pm
FAQ 5.38 Why do I get weird spaces when I print an array of lines? December 22, 2005, 11:03 am
FAQ 5.38 Why do I get weird spaces when I print an array of lines? February 15, 2006, 12:03 pm
FAQ 5.38 Why do I get weird spaces when I print an array of lines? May 4, 2006, 9:03 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap