CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
hash copy only getting 1922 of 1932, no duplicates in read file

 



omega
Novice

Dec 3, 2012, 2:50 AM

Post #1 of 3 (774 views)
hash copy only getting 1922 of 1932, no duplicates in read file Can't Post

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";
}
}
}


FishMonger
Veteran / Moderator

Dec 3, 2012, 7:11 AM

Post #2 of 3 (771 views)
Re: [omega] hash copy only getting 1922 of 1932, no duplicates in read file [In reply to] Can't Post

Please don't post your question in multiple areas. The Beginner area is the appropriate section for this question, but I responded to your duplicate posting in the Intermediate section before seeing this one.


omega
Novice

Dec 3, 2012, 12:46 PM

Post #3 of 3 (756 views)
Re: [omega] hash copy only getting 1922 of 1932, no duplicates in read file [In reply to] Can't Post

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+).

Thank you for the help!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives