Beginner: match /$key/

M

Marek Stepanek

Thank you all, answering my questions in the thread "Beginner: regex-error?"
I appreciate your remarks, also if it is to early for me, to consider style
questions; I am lucky already, if my shell is spitting out some results ...
But I am reading and reading your answers, thinking: Wow! how elegant Perl
code may be by real professionals!

I am close to my original aim. I have two parts of data: first tab-delimited
numbers without dates. Second part tab-delimited text and numbers, starting
with a date. To the first part I add a date with the short name of the day
(in German) and I want to add to these lines the numbers at the end of the
second part, where the dates are the same AND where the line contains oU ...
(later I will calculate with these additional numbers, but this will be the
easy part - I hope!)

My idea was to read in all lines of the second part, containing oU, and
create a hash, where the date is the key, and see later where the keys
matches the lines of the first part.

The following script is working, but strange, only one $key (=date) is
matching, while there should be two!

Before you are suggesting a total other approach, could you help me to make
run this script here, with my idea? - I am sure there is a much more elegant
way to do this, but to learn, it is best for me to finish first one idea and
see later, how to achieve it professionally - the perlish way.

Thank you all, for your patience and the help!


marek


#!/usr/bin/perl

use warnings;
use strict;
use Date::Calc qw:)all);


my (@lines1, @lines2, @lines3);
my %date_number;

while (<DATA>)
{
chomp;
last if /keyword/;
push @lines1, $_;
}
chomp(@lines2=<DATA>);

my $start_day = "15";
my $start_month = "07";
my $year = "2006";
my $day = $start_day;
my $month = $start_month;
my ($wochentag, $date);
my %suplement;

foreach my $line (@lines1)
{
add_one_day();
push @lines3, "$wochentag, $date\t$line";
}


foreach my $line (@lines2)
{
if ($line =~ /oU/)
{
my ($datum) = $line =~ /^([\d.]+)\s/;
my ($number_to_add) = $line =~ /\s([\d.]+)\s*$/;
%suplement = ( $datum => $number_to_add );
}
}

foreach my $line (@lines3)
{
foreach my $key (keys %suplement)
{
if ( $line =~ /$key/)
{
$line .= "\t" . $suplement{$key};
print "the result of concatenation is: $line\n";
}
}
}

sub add_one_day
{

my $day_plus = 1;
my ($year2, $month2, $day2) = Add_Delta_Days($year, $month, $day,
$day_plus);
my $dow = Day_of_Week($year2,$month2,$day2);
Language(Decode_Language("Deutsch"));
my $lang = Language_to_Text(Language());
$wochentag = Day_of_Week_Abbreviation($dow);
$date = sprintf '%02d.%02d.%4d', $day2, $month2, $year2;
($day, $month, $year) = ($day2, $month2, $year2);

}

__DATA__
37086,4 15445 808 19,5 3156,3
37667 15769,2 817 19,5 3621
37936,5 15929,6 823 19,5 3857,8
38147,5 16058 827 19,5 4042,8
38371,8 16188,6 833 19,5 4247,4
38607,7 16252,7 837 19,5 4359,2
38752,7 16351,6 844 20,5 4523,9
38774,2 16351,6 844 20,5 4526,9
39056,9 16499,8 849 20,5 4740,6
39249,2 16574,6 853 20,5 4854,6

39720,7 16762,9 864 21,5 5148,7
39932 16847,6 872 22 5289,8
40002,6 16852,9 874 22 5307
keyword
17.07.2006 CC mU 58.00
17.07.2006 Fr. Knorr CC mU 60.00
18.07.2006 Unterföhring MUC Link CC mU 45.00
19.07.2006 CC mU 58.00
20.07.2006 MUC Hanauer Hermann/Wacki CC mU 55.00
21.07.2006 Claudio/Rock CC oU 55.00
24.07.2006 CC mU 54.00
24.07.2006 CC mU 50.00
24.07.2006 CC mU 55.00
25.07.2006 CC mU 82.00
26.07.2006 -3.7 Uhr! Königin MUC Wacki Bar oU 55.00
27.07.2006 hin rück Link CC mU 122.00
 
A

attn.steven.kuo

Marek Stepanek wrote:

(snipped)
The following script is working, but strange, only one $key (=date) is
matching, while there should be two!

Before you are suggesting a total other approach, could you help me to make
run this script here, with my idea? - I am sure there is a much more elegant
way to do this, but to learn, it is best for me to finish first one idea and
see later, how to achieve it professionally - the perlish way.

Thank you all, for your patience and the help!


marek

(snipped)


foreach my $line (@lines2)
{
if ($line =~ /oU/)
{
my ($datum) = $line =~ /^([\d.]+)\s/;
my ($number_to_add) = $line =~ /\s([\d.]+)\s*$/;
%suplement = ( $datum => $number_to_add );
}
}


You're redefining the contents of the entire hash,
%suplement, instead of adding to it.

Read

%suplement = ( $datum => $number_to_add );

as:

The entire hash, %suplement, consists of a key-value
pair: the key being $datum, and the value being
$number_to add.

To add key-value pairs to the hash, use:

$suplement{ $datum } = $number_to_add;
 
D

Dr.Ruud

Marek Stepanek schreef:
while (<DATA>)
{
chomp;
last if /keyword/;
push @lines1, $_;
}

I would make that:

while (<DATA>)
{
last if /\bkeyword\b/;
chomp;
push @lines1, $_;
}


Or maybe you aimed for /^keyword$/ ?

$/ = "\nkeyword\n" ;
@lines1 = split /\n/, <DATA> ;


my $start_day = "15";
my $start_month = "07";
my $year = "2006";

my ($year, $start_month, $start_day) = ('2006', '07', '15') ;

or just

my ($year, $start_month, $start_day) = (2006, 7, 15) ;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top