Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Perl Hash

by CountOrlok (Friar)
on Apr 17, 2013 at 04:25 UTC ( #1029048=note: print w/ replies, xml ) Need Help??


in reply to Perl Hash

Where to begin...

First, it would make it easier to figure out what is going on if you could reformat the input/output (put them in code tags).

I got several issues with this code but I think the main issue you is that you have a variable named $Summry and one called $Summary which is undefined. Then you have $Detial_Summary which is also undefined

BTW, the first if-else clause is unnecessary because of auto-vivification.

I would rather combine (i.e. join) the fields on an uncommon character like '^' or '|' instead of spaces. It later makes it easier to split the fields back rather than using substr.


Comment on Re: Perl Hash
Re^2: Perl Hash
by Rajsai2825 (Novice) on Apr 17, 2013 at 04:44 UTC
    My code:
    $fulllookupKey = Summary."\t".Detial_Summary."\t".Name."\t".Location." +\t".Sub_Location."\t". <code> if (exists($output{$fulllookupKey})){ $output{$fulllookupKey}->[0] += $Count; $output{$fulllookupKey}->[1] += $Amount; } else { $output{$fulllookupKey}->[0] = $Count; $output{$fulllookupKey}->[1] = $Amount; } for my $key (sort keys %output){ my $Summary = substr($key,0,7); my $Detial_Summary = substr($key,8,7); my $Source = substr($key,16,100); print Out $Summary;." \t" . " \t" . " \t". "\t" ."\t" ."\t" ."\n"; print Out " \t" . $Detial_Summary." \t" . " \t" . " \t". "\t" ."\t" ." +\t" . "\t" ."\n"; print Out " \t" . " \t" . $Source."\t".sprintf("%.2f", $output{$key}-> +[0])."\t".sprintf("%.2f", $output{$key}->[1])."\n"; }
    <INPUT> Summary Detial_Summary EMC CHECK abcdefghi AZ_checker 1 20.32 Summary Detial_Summary EMC CHECK abcdefghi AZ_checker 1 24.32 Summary1 Detial_Summary1 EMC CHECK1 abcdefghi AZ_checker 1 20.59 Summary1 Detial_Summary1 EMC CHECK1 abcdefghi AZ_checker 1 24.00 <INPUT> <OUTPUT>: (what i got) Summary Detial_Summary EMC CHECK abcdefghi AZ_checker 1 20.32 Summary Detial_Summary EMC CHECK abcdefghi AZ_checker 1 24.32 Summary1 Detial_Summary1 EMC CHECK1 abcdefghi AZ_checker 1 20.59 Summary1 Detial_Summary1 EMC CHECK1 abcdefghi AZ_checker 1 24.00 </OUTPUT> Expected Output: <OUTPUT> Summary Detial_Summary EMC CHECK abcdefghi AZ_checker 1 44.32 Summary1 Detial_Summary1 EMC CHECK1 abcdefghi AZ_checker 1 24.59 </OUTPUT>

      Put everything which is code/data inside code tags

        Perl code:
        $fulllookupKey = Summary."\t".Detial_Summary."\t".Name."\t".Location." +\t".Sub_Location."\t". <code> if (exists($output{$fulllookupKey})){ $output{$fulllookupKey}->[0] += $Count; $output{$fulllookupKey}->[1] += $Amount; } else { $output{$fulllookupKey}->[0] = $Count; $output{$fulllookupKey}->[1] = $Amount; } for my $key (sort keys %output){ my $Summary = substr($key,0,7); my $Detial_Summary = substr($key,8,7); my $Source = substr($key,16,100); print Out $Summary;." \t" . " \t" . " \t". "\t" ."\t" ."\t" ."\n"; print Out " \t" . $Detial_Summary." \t" . " \t" . " \t". "\t" ."\t" ." +\t" . "\t" ."\n"; print Out " \t" . " \t" . $Source."\t".sprintf("%.2f", $output{$key}-> +[0])."\t".sprintf("%.2f", $output{$key}->[1])."\n"; }
        <INPUT DATA> Summary Detial_Summary EMC CHECK abcdefghi AZ_checker 1 20.32 Summary Detial_Summary EMC CHECK abcdefghi AZ_checker 1 24.32 Summary1 Detial_Summary1 EMC CHECK1 abcdefghi AZ_checker 1 20.59 Summary1 Detial_Summary1 EMC CHECK1 abcdefghi AZ_checker 1 24.00 <INPUT DATA> <DATA OUTPUT>: (what i got) Summary Detial_Summary EMC CHECK abcdefghi AZ_checker 1 20.32 Summary Detial_Summary EMC CHECK abcdefghi AZ_checker 1 24.32 Summary1 Detial_Summary1 EMC CHECK1 abcdefghi AZ_checker 1 20.59 Summary1 Detial_Summary1 EMC CHECK1 abcdefghi AZ_checker 1 24.00 <DATA /OUTPUT> Expected Output: <EXPECTED OUTPUT> Summary Detial_Summary EMC CHECK abcdefghi AZ_checker 1 44.32 EMC CHECK Grand_Total 2 44.32 Summary1 Detial_Summary1 EMC CHECK1 abcdefghi AZ_checker 1 44.59 EMC CHECK Grand_Total 2 44.32 </EXPECTEDOUTPUT>

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1029048]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2014-08-30 19:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls