Beginner: match /$key/

Discussion in 'Perl Misc' started by Marek Stepanek, Aug 3, 2006.

  1. 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
    Marek Stepanek, Aug 3, 2006
    #1
    1. Advertising

  2. Marek Stepanek

    Guest

    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;

    --
    Hope this helps,
    Steven
    , Aug 3, 2006
    #2
    1. Advertising

  3. On 03.08.2006 07:42, in article
    ,
    "" <> wrote:


    >
    > To add key-value pairs to the hash, use:
    >
    > $suplement{ $datum } = $number_to_add;



    Thank you Steven, I am blushing :)


    Greetings from Munich
    Marek Stepanek, Aug 3, 2006
    #3
  4. Marek Stepanek

    Dr.Ruud Guest

    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) ;


    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Aug 3, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hiwa
    Replies:
    0
    Views:
    631
  2. Victor
    Replies:
    2
    Views:
    629
    Victor
    May 17, 2004
  3. ekzept
    Replies:
    0
    Views:
    356
    ekzept
    Aug 10, 2007
  4. M P
    Replies:
    1
    Views:
    455
  5. John Gordon
    Replies:
    13
    Views:
    463
    Ian Kelly
    Dec 20, 2011
Loading...

Share This Page