Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Help: Print lines

Reply
Thread Tools

Help: Print lines

 
 
Amy Lee
Guest
Posts: n/a
 
      04-24-2008
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
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      04-24-2008
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
 
Reply With Quote
 
 
 
 
Amy Lee
Guest
Posts: n/a
 
      04-24-2008
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
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      04-24-2008
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
 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      04-24-2008
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
 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      04-24-2008
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
 
Reply With Quote
 
January Weiner
Guest
Posts: n/a
 
      04-24-2008
Amy Lee <(E-Mail Removed)> wrote:
[snip]

Use bioperl to parse FASTA files

j.

 
Reply With Quote
 
Amy Lee
Guest
Posts: n/a
 
      04-25-2008
On Thu, 24 Apr 2008 11:52:24 +0100, RedGrittyBrick wrote:

> 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


Thank you. But there's a problem I can't understand. What if I hope create
files like CT1 contains the CT1 label including; CT2 contains the CT2
label including and so on. However, I think I should read the label.

How to accomplish that?

Thank you very much~

Regards,

Amy Lee
 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      04-25-2008
Amy Lee wrote:
> On Thu, 24 Apr 2008 11:52:24 +0100, RedGrittyBrick wrote:
>
>> 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

>
> Thank you. But there's a problem I can't understand. What if I hope create
> files like CT1 contains the CT1 label including; CT2 contains the CT2
> label including and so on. However, I think I should read the label.
>
> How to accomplish that?
>


I'm not sure I understand what you mean - it would be clearer if you
give an example of the data.

Did you mean
>CT1

XY0002658-96
0000222541
CT1
4444444444
5555555555
>CT2

XY0002688-55
0000254147
CT1
CT2
5555555555
6666666666
7777777777
>CT5

ZZ0004854-00
0000475568
CT2
CT5
5555555555
6666666666

If so, my suggested script would split the records OK because it uses
newline space greater-than as the record separator. It however would
select the wrong records because the selector is now insufficiently
precise. We want to match CT2 (say) only when occurs at the start of a
record. You can use the ^ character to anchor an expression to the
start. "/CT2.../" becomes "/^CT2.../"

Do read the documentation - you will be able to work a lot of this out
yourself.

perldoc perlre
perldoc perlop (look for "m/PATTERN")

--
RGB
 
Reply With Quote
 
Amy Lee
Guest
Posts: n/a
 
      04-25-2008
On Fri, 25 Apr 2008 14:59:17 +0100, RedGrittyBrick wrote:

> Amy Lee wrote:
>> On Thu, 24 Apr 2008 11:52:24 +0100, RedGrittyBrick wrote:
>>
>>> 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

>>
>> Thank you. But there's a problem I can't understand. What if I hope create
>> files like CT1 contains the CT1 label including; CT2 contains the CT2
>> label including and so on. However, I think I should read the label.
>>
>> How to accomplish that?
>>

>
> I'm not sure I understand what you mean - it would be clearer if you
> give an example of the data.
>
> Did you mean
> >CT1

> XY0002658-96
> 0000222541
> CT1
> 4444444444
> 5555555555
> >CT2

> XY0002688-55
> 0000254147
> CT1
> CT2
> 5555555555
> 6666666666
> 7777777777
> >CT5

> ZZ0004854-00
> 0000475568
> CT2
> CT5
> 5555555555
> 6666666666
>
> If so, my suggested script would split the records OK because it uses
> newline space greater-than as the record separator. It however would
> select the wrong records because the selector is now insufficiently
> precise. We want to match CT2 (say) only when occurs at the start of a
> record. You can use the ^ character to anchor an expression to the
> start. "/CT2.../" becomes "/^CT2.../"
>
> Do read the documentation - you will be able to work a lot of this out
> yourself.
>
> perldoc perlre
> perldoc perlop (look for "m/PATTERN")

Thanks your reply. I suppose that I can write a bit if possible. My
meaning is if my file contains CT1, CT2, CT5 these three entry, then I
can make 3 files called CT1, CT2, CT5 and CT1 file contains the words in
CT1 entry, CT2 file contains the words in CT2 entry, and so on.

I will paste my codes if I meet questions.

Thank you again.

Regards,

Amy
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
print a vs print '%s' % a vs print '%f' a David Cournapeau Python 0 12-30-2008 03:19 AM
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
Asp.Net Calender, how to display 5 lines if there are only 5 lines in one month? Jack ASP .Net 9 10-12-2005 03:44 AM
Unlarging the print to print using PDF file to print Bun Mui Computer Support 3 09-13-2004 03:15 AM
Why does "while (<>) { $x .= $_; } print $x . "\n";" print all input lines? Wolfgang Perl Misc 1 02-13-2004 07:51 PM



Advertisments