My problem is about a merge need between the content of two or more text lines...
I'll try to explain in a "better way".
Let's suppose that I have a "table" formatted in this way:
(please visualize it with no-proportional font)
Code date-in date-out time-in time-out state
11111 05-10-03 10.10
11111 05-10-03 12.15
11111 open
22222 05-11-03 08.20
22222 05-12-03 11.05 closed
what I'd like to have is something which format all like this:
Code date-in date-out time-in time-out state
11111 05-10-03 05-10-03 10.10 12.15 open
22222 05-11-03 05-12-03 08.20 11.05 closed
Do you think it will be simple to do it? ;)
Thanks in advance for your attention.
.e.
Use a loop to loop through the lines, collecting the information relating to each code; and print it out everytime you encounter a new code, or you reach the end of the data.
You can extract each line into variables using the split function. Just read it in in the order:
code date-in time-in date-out time-out state
(but ignoring the new lines and the repetition of the code at the start of every line)
Then write it out in the order you want it, and go to the next code.
Shawn
Sure, 'though for future posts please bear in mind that if you are interested in learning Perl, you'd probably get up to speed quicker by trying something and then posting when you hit a stumbling block.
Here is one way. Others might suggest more elegant ways:
open DATAFILE 'filename';
my $code = "";
my $i = 0;
my @alarm_data;
while (<DATAFILE>)
{
____@words_in_line = split;
____$current_line_code = shift @words_in_line;
____if ($code!= $current_line_code) {
________# about to start a new code, so print out the previous one...
________print "$code $alarm_data[0] $alarm_data[2] $alarm_data[1] $alarm_data[3] $alarm_data[4]\n";
________$code = $current_line_code;
________$i=0;
____}
____@alarm_data[i.. (i + $#words_in_line - 1)] = @words_in_line;
____i += $#words_in_line;
}
# print the last one:
if ($code) { # there was at least one
____print "$code $alarm_data[0] $alarm_data[2] $alarm_data[1] $alarm_data[3] $alarm_data[4]\n";
}
(Untested)
Limitations I can think of:
Shawn
--------------------------
my @nomi_campi = qw(col1 col2 col3 col4 col5 col6);
my @cont_campi;
my $chiave;
my %record;
#
sub scrivi_record;
#
$FileName = $ARGV[0];
open(FILE,"< $FileName");
print $nomi_campi[0];
#
for (my $ind = 1; $ind <= $#nomi_campi; ++$ind) {
print "\t", $nomi_campi[$ind];
}
print "\n";
#
while (<FILE>) {
chomp();
@cont_campi = split(/\t/);
if (!defined($chiave)) {
$chiave = $cont_campi[1];
}
elsif ($chiave ne $cont_campi[1]) {
scrivi_record;
undef %record;
$chiave = $cont_campi[1];
}
for (my $ind_campo = 0;$ind_campo <= $#cont_campi;++$ind_campo) {
if (defined($cont_campi[$ind_campo]) and $cont_campi[$ind_campo] ne '') {
$record{$nomi_campi[$ind_campo]} = $cont_campi[$ind_campo];
}
}
}
# scrive l'ultima riga
scrivi_record;
#
close FILE;
#
sub scrivi_record {
print $record{$nomi_campi[0]};
for (my $ind = 1; $ind <= $#nomi_campi; ++$ind) {
print "\t", $record{$nomi_campi[$ind]};
}
print "\n";
}
#
------------
for a better understandig the translation for "nomi_campi" is "field_name", for "cont_campi" is "fields_content", for "chiave" is "key", for "scrivi_record" is "print_record"...
Hope this help someone with same needs...
Thanks for your support!