Forum Moderators: coopster & phranque

Message Too Old, No Replies

merge of two (or more) text lines...

I need to make a merge between two or more text lines to obtain one line...

         

poitelodico

9:10 am on May 22, 2003 (gmt 0)

10+ Year Member



Hi!
this is my first post in this forum... I'm from Italy and I've found you by Google...

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.

dmorison

9:30 am on May 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there!

It shouldn't be too difficult - but you need to give more information about the scenario.

Are you trying to lay this out in HTML, or are you using PHP or Perl to process some data?

poitelodico

9:39 am on May 22, 2003 (gmt 0)

10+ Year Member



I don't need them for html layout...
I'm trying to do it by Perl and for data process.
The record are extracted from a data base using Perl and some API of the application we use to manage them.

thanks for your attention...

.

ShawnR

1:59 pm on May 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest:

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

poitelodico

2:53 pm on May 22, 2003 (gmt 0)

10+ Year Member



It sounds simple... but...
Please, could you write down a some code lines from which I could start to develop that?

thanks

ShawnR

10:07 am on May 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>>"...could you write down a some code lines ..."

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:

  • no validation of the data in the datafile.
  • assumes all events for a code are grouped together. i.e. you can't have 123 in, 127 in, 123 out, 127 out.

Shawn

poitelodico

2:38 pm on May 23, 2003 (gmt 0)

10+ Year Member



thanks a lot for you advice...
I did some try and, at the end, I've found the answer to my needs...
This is the final (working) code:


--------------------------
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!

jatar_k

4:43 pm on May 23, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



glad to see you got it working poitelodico,

and since no one else mentioned it
Welcome to WebmasterWorld

poitelodico

7:18 am on May 26, 2003 (gmt 0)

10+ Year Member



Thank you very much!

I hope to be able to help somebody in the future.

ciao