Looking for a perl script example...

MIDIman

Diamond Member
Jan 14, 2000
3,594
0
0
I have a tab delimited text file. I'm looking for a perl example that basically parses the data between each tab as a variable one line ata time (an array I would assume), reformats a column of data, then spits it back out as a tab delimited text file, in a different order.

Just looking for some code examples to at least get me started. Thanks in advance.
 

Mitzi

Diamond Member
Aug 22, 2001
3,775
1
76
Edit: Read my second post instead! It makes more sense!!


I only have a few minutes because I'm at work so I'll just post this but at least it should give you at least a few hints....


#! /usr/bin/perl

open (FILEHANDLE, "test.dat");
while ($record = <FILEHANDLE>) {
chop $record;
@field = split(/\t/, $record);
print "Element 0: $field[0]\n"; #can access any element of the array like this
# do whatever processing you want on the field array here
}
close(FILEHANDLE);


The above code will load a file called test.dat (which is tab delimited) and read each element of each line by line into an array called field. You can then process each line by line.

If you want to read the entire file into one array change the line' $record= <FILEHANDLE> to...

@myfile = <FILEHANDLE>;

Hope that helps.
 

Mitzi

Diamond Member
Aug 22, 2001
3,775
1
76
Actually ignore my last post, this is a little bit more what you need. It opens an input file (called input.dat), reads each line from that file and writes each line (reversing the elements) to a file called output.dat.

Hope the explaination makes sense!


#! /usr/bin/perl

open (INPUT, "input.dat");
open (OUTPUT, ">output.dat");
while (<INPUT>) {
chop $_;
@field = split(/\t/, $_);
print OUTPUT "$field[1]\t$field[0]\n";
}
close(OUTPUT);
close(INPUT);

 

MIDIman

Diamond Member
Jan 14, 2000
3,594
0
0
Very nice thanks - I'll get cracking for a bit, and probably re-post tomorrow with added troubles...
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
# Create array to store data
my @data;

# Read data from tab-delimited file
open FILE, "in.txt"; # open inut file
while (<FILE>){ # for each line in the file:
chomp $_; # Rremove trailing newline
my @line = split /\t/, $_; # split at tabs
push @data, \@line; # Add to data array
}
close FILE; # close input file

# Process array data here

# Write data to comma-delimited file
open FILE, ">out.txt"; # Open output file for writing
foreach $line(@data){ # for each line in the array:
print FILE join(',', @$line), "\n"; # Join all the elements w/ a ',' and print a line to the output file.
}
close FILE; # close the output file

 

MIDIman

Diamond Member
Jan 14, 2000
3,594
0
0
Very cool...quick question, since I don't know arrays super well...

How can I modify a single column - i.e. one particular tab position in each line...for instance, say if tab position number 6 was Beta, how can I change that to a number like 10?

Thanks a bunch...I love this forum!
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
In my example, where it says "# Process array data here" you add something like this:

for(my $ii = 0; $ii < @data; $ii++){
if($data[$ii][5] eq "Beta"){
$data[$ii][5] = 10;
}
}