Read and Parse a Tab Delimited Text File

The following Perl script will read all text files in the current working directory and then print columns 1 and 2 in a separate output file named “result.out”.

#!/usr/bin/perl -w

#William DiStefano
#Version: 1

use Cwd;
my(@handles);

print "Process starting...\n";

unlink "result.out"; #remove result file if it exists

for(<./*.txt>){
  open($handles[@handles], $_);
}

my$continue = 1;

while ($continue) {
  $continue = 0;
  for my$op(@handles) {
    my@col = split;
    print OUTFILE $col[0] . "\t" . $col[1];
    $continue = 1;
  }
  print OUTFILE "\n";
}

undef@handles; #close tab-delimited files
close(OUTFILE); #close result.out

print "Process Complete!";

 

A Very Simple Way to Parse XML Files

#!/usr/bin/perl -w
 use strict;
 use XML::Simple;
use Data::Dumper;
my $smpl = XML::Simple->new();
 my $d = $smpl->XMLin('file.xml');
print Dumper($d) . "\n";


Share