#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

    Hash copy only getting 1922 of 1932 keys with no duplicates in origin file?


    After debuging I found the issue to be in:

    if ($month =~ /\d\d/) { # keep double digit months
    $just = "$year" . '_' . "$month" . '_' . "$day";
    $new_numbers{$just} = "$numbers{$bookmark}";
    } else { # change single digit months to include a '0'
    ($new_month = $month) =~ s/(\d)/0\1/;
    $work = "$year" . '_' . "$new_month" . '_' . "$day";
    $new_numbers{$work} = "$numbers{$bookmark}";
    }

    where this code sees to be finding duplicates but the above portion finds none. How can one hash have a duplicate key if the original hash is only having a 0 added on to single digit numerals?

    below is the complete code:



    #!/usr/bin/perl

    # 1932

    open LOTTO, '<', 'lotto.txt' # make 'lotto.txt' the sample file
    or die "couldnt open file";

    while ( <LOTTO> ) { # open every line of 'lotto.txt'
    @trial1 = split /\s+/, "$_"; # remove all the spaces
    $trial2 = "@trial1"; # change array to scalar

    if ( $trial2 =~ m{(\d+)/(\d+)/(\d+).*(\d+)/(\d+)/(\d+)} ) {
    $dates_one = "$1_$2_$3"; # look for date 1 in current line
    $dates_two = "$4_$5_$6"; # look for date 2 in current line
    }
    if ( $trial2 =~ /(\d+) - (\d+) - (\d+) - (\d+) - (\d+) - (\d+).*(\d+) - (\d+) - (\d+) - (\d+) - (\d+) - (\d+)/ ) {
    $numbers{$dates_one} = "$1-$2-$3-$4-$5-$6"; # look for numbers 1 in current line
    $numbers{$dates_two} = "$7-$8-$9-$10-$11-$12"; # look for numbers 2 in current line
    }
    }

    foreach (keys %numbers) { # formating program ( dates )
    $bookmark = "$_";

    if (/(\d+)_(\d+)_(\d+)/) {
    $month = $1;
    $day = $2;
    $year = $3;

    if ($month =~ /\d\d/) { # keep double digit months
    $just = "$year" . '_' . "$month" . '_' . "$day";
    $new_numbers{$just} = "$numbers{$bookmark}";
    } else { # change single digit months to include a '0'
    ($new_month = $month) =~ s/(\d)/0\1/;
    $work = "$year" . '_' . "$new_month" . '_' . "$day";
    $new_numbers{$work} = "$numbers{$bookmark}";
    }
    }
    }

    foreach (keys %new_numbers) { # formating program ( numbers )
    $bookmark = "$_";

    if ($new_numbers{$bookmark} =~ /(\d+)-(\d+)-(\d+)-(\d+)-(\d+)-(\d+)/) {
    $one = $1;
    $two = $2;
    $three = $3;
    $four = $4;
    $five = $5;
    $six = $6;

    if ($one =~ /\d\d/) { # change single digit pairs to include a '0'
    } else {
    $one = '0' . "$one";
    }
    if ($two =~ /\d\d/) {
    } else {
    $two = '0' . "$two";
    }
    if ($three =~ /\d\d/) {
    } else {
    $three = '0' . "$three";
    }
    if ($four =~ /\d\d/) {
    } else {
    $four = '0' . "$four";
    }
    if ($five =~ /\d\d/) {
    } else {
    $five = '0' . "$five";
    }
    if ($six =~ /\d\d/) {
    } else {
    $six = '0' . "$six";
    }

    $new_numbers{$bookmark} = "$one-$two-$three-$four-$five-$six";
    }
    }

    foreach ( sort keys %new_numbers ) { # sorting program ( pre-y2k )
    $bookmark = "$_";
    if ( /(\d+)_\d+_\d+/ ) {
    $year = $1;

    if ( $year > 13 ) {
    print "$bookmark: $new_numbers{$bookmark}\n";
    }
    }
    }

    foreach ( sort keys %new_numbers ) { # sorting program ( post-y2k )
    $bookmark = "$_";
    if ( /(\d+)_\d+_\d+/ ) {
    $year = $1;

    if ( $year < 13 ) {
    print "$bookmark: $new_numbers{$bookmark}\n";
    }
    }
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,932
    Rep Power
    1225
    See my answer in your cross post on perlguru.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    830
    Rep Power
    496
    Use the sprintf function to reformat your number strings.

    Don't use a syntax like:

    Code:
    $one = $1;
    $two = $2;
    $three = $3;
    # ...
    but put all your captured values into an array.

    As an example:

    Code:
    my $foo = '11-22-33-44';
    my @result = $foo =~ /(\d+)-(\d+)-(\d+)-(\d+)/;
    print "@result"; # prints "11 22 33 44"
    And now that your values are in an array, instead of multiple variables, you will be able to process all of them with just one line of code or two (using sprintf for formatting and foreach or map for processing each value, instead of about 25 lines of code to process each one by one.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

    solved


    problem fixed, the issue was in (\d+).*(\d+) only taking the first digit of $2(in that example) in both the date and the numbers, fixed with: (\d+).*\s(\d+).

    I knew you could use: (@some = $thing) =~ ... with s/// but i didnt know it could be used for other regular functions so thanks for the tip. and about sprintf im only half-way through the book so im relying on tmotdi and since this was a logical error issue and not a function problem i was <hopeing> it would be appropriate here.

    thank you for your structure advice and general help!

IMN logo majestic logo threadwatch logo seochat tools logo