open(INFILE, "in_fileName.txt") || die("Can't open in_fileName.txt"); ... close (INFILE)
You are in the headquaters of avalanche rescue mission. Ground crews and helicopter crews are looking for the victims. You open the CB radio connection to the ground crews through Channel 1 and to the helicopter crews to Channel 2. You open these different connection channel, so you know whom you are talking to. File handles are like these different channels, each channel get a filehandle name such as INFILE1, OUT1, TEMPFILE etc.
But if A fails, B will be evaluated (get executed).
If it fails (e.g. wrong permission, no file with the filename exists), it returns 0 (False). So it will execute die(), which print out the string, and exit the process.
Always use ``|| die()'' after open() to make sure opening was successful
open (OUTFH, "> fileName") || die "Can't open output file"; # Write access open (OUTFH2, ">> fileName") || die "Can't open output file"; # Append access open (INFH, "< fileName") || die "Can't open input file"; # Read accessNote that ``<'' is optional for read access (default behavior).
my $line = <INFILE>; # first line my $line2 = <INFILE>; # second line my @lines = <INFILE>;
Why don't you open a file (whatever text file in your home directory), read in contents using scalar and array context, and print it out?
while(<INFILE>) { # assign each line to $_ chomp ($_); print "$.: $_\n"; }This is a very common way of processing data.
\
n) from the string $var.
while(<INFILE>) { # assign each line to $_ chomp; # Removing '\n' from $_; print "$.: "; print; # Printing $_; print "\n"; }
#!/usr/bin/perl -w # getCol.pl colNum file # Extract one column from a tab-delimited file. # The program takes two arguments: # 1. The column number to extract # 2. Name of file my ($colNum, $fileName) = @ARGV; open IN, "< $fileName" || die "Can't open $fileName"; while(<IN>) { chomp; my @line = split(/\t/, $_); # split tab delimited line ($_) and make an array print $line[$colNum - 1], "\n"; } close(IN); exit;
If you do the following in the command line
./runThis.pl 3 filename1 filename2
you'll get @ARGV = (3, "filename1", "filename2").
Try this:
#!/usr/bin/perl -w print "Printing argument array: @ARGV\n"; exit;
my $s = "1,3,2,0"; my @a = split (/,/, $s); # it becomes @a = (1, 3, 2, 0) $s = "ATGCT"; @a = split //, $s; # it becomes @a = ("A", "T", "G", "C", "T")
open (OUT_FH, ">outfile"); print OUT_FH "Write this to a file\n"; close (OUT_FH);
>llama
GGTCTTATATTTTATATT
>camel
ATATGATC
GACTTTAC
CCCTCTT
>owl
TTTTCTGAT
TTGCTG
Then
./fastaNames.pl animals.fasta
should print out:
>llama
>camel
>owl