Front page | perl.beginners |
Postings from December 2002
foreach, if/else, and concatenation
Thread Next
From:
Albert L. Lukaszewski
Date:
December 21, 2002 12:14
Subject:
foreach, if/else, and concatenation
Message ID:
20021221174746.A9727@localhost.localdomain
At the earlier suggestion of Rob Dixon (rob[at]dixon.nildram.co.uk), I
here present a sample of the data I am trying to convert, the output I
desire, and the program as I have written it thus far.
I am using "4Q246.db" as a sample of the larger file. In between each
text, I want it to add three CRs, two blank lines, between the end of
the one text and the title of the next. Ultimately, I would love it
if they could all go into files according to their names, but I am
pressed for time and just need the second field for each line collated
for right now.
Any help is greatly appreciated. Thanks.
Al
DATA:
4Q246.1.1, yhwl[(], prep.|s5, prep.loc~,
4Q246.1.1, tr#, v-G11, v., <yr# to settle
4Q246.1.1, lpn, v-G10, v., <lpn to fall
4Q246.1.1, Mdq, prep., prep.locative, <Mdq before
4Q246.1.1, )ysrk, n-mse, p.obj., <)srk chair
4Q246.1.2, )kl[m], n-mse, vocative, <Klm king
4Q246.1.2, )ml(<m>[l], prep.|n-mse, prep.temporal|p.obj., <Ml( forever
4Q246.1.2, ht), p2, s., <ht( you
4Q246.1.2, zgr, adj-msa, adj.modifier, <zgr angry/distressed
4Q246.1.2, Kyn#w, c.conj.|n-mpc|s2, s., <yn# year
4Q246.1.3, ...,, ,
4Q246.1.3, Kwzx, n-msc|s2, , <wzx vision
4Q246.1.3, )lkw, c.conj.|adj-mse, adj.substantive,
4Q246.1.3, ht), p2, s., <ht) you
4Q246.1.3, d(, prep., prep.temporal, <d( until
4Q246.1.3, )ml(, n-mse, p.obj., <Ml( forever
OUTPUT (right justified eventually):
)ysrk Mdq lpn tr# yhwl[(] 4Q246.1.1
Kyn#w zgr ht) )ml(<m>[l] )kl[m] 4Q246.1.2,
)ml( d( ht) )lkw Kwzx ... 4Q246.1.3
PROGRAM:
#!/usr/bin/perl
#############################################################
#############################################################
# A PROGRAM TO READ THE QUMRAN ARAMAIC DATABASE #
# AND CONCATENATE IT BY TEXT NAME INTO #
# NUMBERED LINES, PROPERLY FORMATTED FOR A CHRESTOMATHY #
#############################################################
#############################################################
#use strict;
#use warnings;
###################################
# OPEN THE INPUT AND OUTPUT FILES #
###################################
my $file;
$file = '4Q246.db' ; # Name the input file
open(INFO, "$file" ) or die"Cannot open $file:$!\n"; # Open the input file or report failure
#my @db;
#@db = <INfo> ; # Read it into an array
#close(INFO) ; # Close the input file
open OUT, ">>qa.txt" or die "Cannot open file 'qa.txt'!\n"; # Open the output file
########################################
# INITIALIZATION OF SCALARS AND ARRAYS #
########################################
my $line; # = scalar by which program steps through data
my $fieldEval = "null"; # = holding scalar for evaluating whether the line reference has changed
my $fieldCtrl = "null"; # Preset scalar to 'zero' for evaluating whether the line reference has changed
my @field;
#my @outbound = ""; # = holding array for output of line - but not line reference
my $lineOutCounter; # counter for how many times a value is added to @lineOut
#######################################################
# FOREACH CONTROL TO READ THE INPUT FILE LINE BY LINE #
# AND MANIPULATE THE DESIRED DATE TO AN OUTPUT FILE #
#######################################################
foreach $line (<INFO>) { # Assign the contents of the input file to $line one line at time for evaluation.
chomp ($line);
my @lineOut = ""; # = holding array to which forms will be appended right to left
@field[0..4] = split /,/, $line; # Read each line as five fields split by commas
$fieldEval = @field[0]; # Place/Copy line reference into evaluation scalar
print "@field[0] \t @field[1] \t $fieldEval \t @lineOut\n";
if ($fieldCtrl ne $fieldEval){ # Evaluate whether line reference has changed
$fieldCtrl = @field[0]; # IF SO, assign the new line reference to the control scalar
printf OUT "\n\n\n"; # Print 3 carrige returns to output file - this will result in two blank lines
printf OUT " ", @field[0]; # Print tabspace and the new line reference
@lineOut eq @field[1];
print "\t @lineOut \t @field\n";
}
else
{
foreach $word ($line){
@lineOut = unshift @lineOut, " "; # Append a blank space to the beginning of lineOut
@lineOut = unshift @lineOut, @field[1]; # Append the form of the current line to the beginning of lineOut
$lineOutCounter = $lineOutCounter + 1; #####
}
}
# print "@lineOut"; #####
printf OUT "@lineOut \n"; # Print the lineOut array to the output file
}
#print "\n @field \n";
close (OUT); # Close the output file
close(INFO) ; # Close the input file
__END__
--
Albert Lukaszewski
University of Saint Andrews
Saint Andrews, Fife
KY16 9JU
Thread Next
-
foreach, if/else, and concatenation
by Albert L. Lukaszewski