Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

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";
#!/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"; }

Replies are listed 'Best First'.
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

      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: $!";
      I did go through it as advised. will the updated code work and if not why?
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

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1041657]
Approved by GrandFather
[stevieb]: yeah, so the wiringPi library appears to be missing/injecting incorrect defined variables into I2C calls, and it's all over the map. I'm going to have to revisit and use something else, write something else, or scrutinize the code and fix

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2017-06-23 00:06 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (532 votes). Check out past polls.