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: Intermediate:
hash copy only getting 1922 of 1932, no duplicates in read file

 



omega
Novice

Dec 3, 2012, 2:43 AM

Post #1 of 4 (1524 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:07 AM

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


Quote

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



Ouch, that really hurts my eyes. You need to learn to use sprintf and printf.


Code
$date = sprintf("%d_%02d_%02d", $year, $month, $day); 
$new_numbers{$date} = $numbers{$bookmark};


Similar improvements could be made in other sections of your code.

Also, you should always include these 2 lines (pragmas) in every script.

Code
use strict; 
use warnings;

Doing so will require you to declare your vars, which is done with the 'my' keyword.

example:
code]@trial1 = split /\s+/, "$_";


should be:

Code
my @trial1 = split /\s+/, $_;



omega
Novice

Dec 3, 2012, 9:08 AM

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

thank you for your help and your dedication to helping a wide range of people on multiple forums, yes i realize this was meant for beginners but i couldn't find a way to move it myself.

i will look into your suggestions immediately, and thank you again.


omega
Novice

Dec 3, 2012, 12:42 PM

Post #4 of 4 (1490 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