Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

print problem

by yueli711 (Sexton)
on Jun 15, 2018 at 23:01 UTC ( [id://1216755]=perlquestion: print w/replies, xml ) Need Help??

yueli711 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I just want to print a line which contains "Homo sapiens". Thanks in advance!

open(IN1,"tmp01.txt") || die "Cannot open this file"; @lines1 = <IN1>; open(OUT,">tmp03") || die "Cannot open this file"; for $item1(@lines1){ chomp $item1; @tmp1=split(/\t+/, $item1); if ($tmp1[4] eq "Homo sapiens"){ print OUT @lines1; last } print OUT "\n"; } close(IN1); close(OUT);

Replies are listed 'Best First'.
Re: print problem
by 1nickt (Canon) on Jun 15, 2018 at 23:17 UTC

    More Perlishly:

    use strict; use warnings; use autodie; open my $IN, '<', 'in.txt'; open my $OUT, '>', 'out.txt'; while ( my $line = <$IN> ) { print $OUT $line if $line =~ /Homo Sapiens/; } close $IN; close $OUT; __END__

    Hope this helps!

    Edit: removed the chomp to print the linebreaks


    The way forward always starts with a minimal test.

      Thank you so much!

Re: print problem
by Paladin (Vicar) on Jun 15, 2018 at 23:20 UTC
    This works just fine when I run it locally. Can you provide a sample of your tmp01.txt file? The issue would seem to be related to it.

      Thank you so much!

Re: print problem
by yueli711 (Sexton) on Jun 15, 2018 at 23:11 UTC
    open(IN1,"tmp01.txt") || die "Cannot open this file"; @lines1 = <IN1>; open(OUT,">tmp03") || die "Cannot open this file"; for $item1(@lines1){ chomp $item1; @tmp1=split(/\t+/, $item1); if ($tmp1[4] eq "Homo sapiens"){ print OUT $item1; } } close(IN1); close(OUT);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1216755]
Approved by taint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-03-29 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found