Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

How to read from a .txt

by stamp1982 (Novice)
on Jun 30, 2013 at 21:49 UTC ( #1041657=perlquestion: print w/ replies, xml ) Need Help??
stamp1982 has asked for the wisdom of the Perl Monks concerning the following question:

Hello perl monks; How can I get the code to read from a file? It keeps asking me to enter sequence at line 4 but I need it to read from a file .txt file on my desktop.

How can I incorporate this into a code?

# Open a .txt file open(seq1, "<seq1.txt") or die $!; #Assign open files to arrays my @seq1 = <seq1>; print "@seq1";
download
#!/usr/bin/env perl use warnings; use strict; # Open a .txt file open(seq1, "<seq1.txt") or die "Please enter two sequences as argumen +ts on the command line\n" unless @ARGV == 2; my ($orig, $comp) = map { uc } @ARGV; die "Please enter only ATCG sequences\n" if grep { /[^ATCG]/ } $orig, +$comp; die "Sorry, the two sequences you have just entered are of different l +engths." . "\nPlease try again on the command line.\n" if length $orig != length $comp; # $comp will transform back to $orig iff it is the reverse complement $comp =~ y/ATCG/TAGC/; if ($orig eq reverse $comp) { print "Yes, the two sequences are reverse-complement of each other +.\n"; exit; } else { die "Unfortunately, the two sequences are not reverse-complement.\ +n"; }

Comment on How to read from a .txt
Select or Download Code
Re: How to read from a .txt
by Anonymous Monk on Jun 30, 2013 at 22:01 UTC
    write some code to do it with help from perlintro
      I did go through it as advised. will the updated code work and if not why?

      If I started with some thing like this how will I join it to my code to work or make sense.

      # Open a .txt file open(seq1, "<seq1.txt") or die $!; #Assign open files to arrays my @seq1 = <seq1>; print "@seq1";
        Didn't try your code, but the syntax for open is usually this:
        open(my $fh, "<", "input.txt") or die "cannot open < input.txt: $!";
Re: How to read from a .txt
by Jim (Curate) on Jun 30, 2013 at 23:06 UTC

    Your Perl script runs fine for me. Don't change anything. Use it exactly as it is.

Re: How to read from a .txt
by cyates (Novice) on Jul 01, 2013 at 02:30 UTC

    If it's directly from a txt file, you might have better luck with the $_ variable

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2014-09-17 05:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (58 votes), past polls