By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,678 Members | 2,021 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,678 IT Pros & Developers. It's quick & easy.

creating and sorting records from pseodofile using hash

P: 6
I'd like to get the following output from the program below:
Expand|Select|Wrap|Line Numbers
  1. Ref:total Orig:0 total Dupes:0 total Printed:0
  2. Ref:000046 total Orig:2 total Dupes: 0 total Printed:2
  3. Ref:000077 total Orig:6 total Dupes: 12 total Printed:18
  4. Ref:000146 total Orig:2 total Dupes: 0 total Printed: 2
  5. Ref:000243 total Orig:1 total Dupes: 4 total Printed: 5
  6. Ref:000249 total Orig:5 total Dupes: 0 total Printed:5
  7. Ref:100041 total Orig:1 total Dupes: 3 total Printed: 4
  8. Ref:100045 total Orig:3 total Dupes:0 total Printed:3
The program starts from here:This is what i have tried but it doesn't give me the required output. Can you please make necessary changes in my code and help me get the required output. Thank you in advance.

Expand|Select|Wrap|Line Numbers
  1. my %myhash;
  2. my @recordArray = (<DATA>);
  3. my $numpages;
  4. my $refNum;
  5. my $dupInd;
  6. while ($line = <DATA>)
  7.      {
  8.       @recordArray = split />/,$line;
  9.       $numpages = $recordArray[1];
  10.       $refNum = $recordArray[2];
  11.       $dupInd = $recordArray[5]=="C";
  12.       $myHash({$refNum}.{Total})++ = $numpages;
  13.       $myHash{$refNum}{$dupInd? "Dupes":"Originals"}++ = $numpages;
  14.       }
  15. foreach (sort values %myhash)
  16.       {
  17.        printf "Ref: %f total Orig: %d total Dupes: %d total Printed: %d\n", $/,$myHash{$_}{Originals},$myHash{$_}{Dupes},$myHash{$_}{Total};
  18.       }
  19. __DATA__
  20. REC>0002>000046>18000>TSOP>M>18000000046>DO
  21. REC>0006>000077>18000>TSOP>M>18000000046>DO
  22. REC>0006>000077>18000>TSOP>C>18000000046>DO
  23. REC>0006>000077>18000>TSOP>C>18000000046>DO
  24. REC>0005>000249>18000>TSOP>M>18000000046>DO
  25. REC>0001>100041>18000>TSOP>M>18000000046>DO
  26. REC>0001>100041>18000>TSOP>C>18000000046>DO
  27. REC>0001>100041>18000>TSOP>C>18000000046>DO
  28. REC>0001>100041>18000>TSOP>C>18000000046>DO
  29. REC>0002>000146>18000>TSOP>M>18000000046>DO
  30. REC>0001>000243>18000>TSOP>M>18000000046>DO
  31. REC>0001>000243>18000>TSOP>C>18000000046>DO
  32. REC>0001>000243>18000>TSOP>C>18000000046>DO
  33. REC>0001>000243>18000>TSOP>C>18000000046>DO
  34. REC>0001>000243>18000>TSOP>C>18000000046>DO
  35. REC>0003>100045>18000>TSOP>M>18000000046>DO
Jan 25 '09 #1
Share this Question
Share on Google+
9 Replies


KevinADC
Expert 2.5K+
P: 4,059
Fix all the syntax errors and the code might work.
Jan 25 '09 #2

P: 6
Thanks KevinADC for responding. I am not able to fix the syntax. When i try to make some changes to fix the syntax. it doesn't do anything. Can you please give me some suggestion on the script i have written.
Jan 25 '09 #3

KevinADC
Expert 2.5K+
P: 4,059
Post the code with the fixes that you tried but that did not work.
Jan 25 '09 #4

P: 6
Hi KevinADC
Actually i am not sure about the usage of Total in the above program. Can you please make necessary changes in the script i have posted. I am lost i am randomly making changes to fix errors which may not make sense to you.
Thanks
Jan 25 '09 #5

KevinADC
Expert 2.5K+
P: 4,059
Not only don't you understand the usage of Total, you seem to not understand many other things. Is this school work?
Jan 25 '09 #6

KevinADC
Expert 2.5K+
P: 4,059
This is a one time effort on my part to help you unless you demonstarte more effort in the future:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. my %myHash;
  4. while (my $line = <DATA>)
  5.      {
  6.       my ($numpages, $refNum, $dupInd) = (split />/,$line)[1,2,5];
  7.       $myHash{$refNum}{'Total'} += $numpages;
  8.       $myHash{$refNum}{'Orig'}  += $numpages if ($dupInd eq 'M');
  9.       $myHash{$refNum}{'Dupes'} += $numpages if ($dupInd eq 'C');
  10.       }
  11. foreach my $key (sort keys %myHash)
  12.       {
  13.        printf "Ref: %06d total Orig: %d total Dupes: %d total Printed: %d\n", $key,$myHash{$key}{'Orig'},$myHash{$key}{'Dupes'},$myHash{$key}{Total};
  14.       }
  15. __DATA__
  16. REC>0002>000046>18000>TSOP>M>18000000046>DO
  17. REC>0006>000077>18000>TSOP>M>18000000046>DO
  18. REC>0006>000077>18000>TSOP>C>18000000046>DO
  19. REC>0006>000077>18000>TSOP>C>18000000046>DO
  20. REC>0005>000249>18000>TSOP>M>18000000046>DO
  21. REC>0001>100041>18000>TSOP>M>18000000046>DO
  22. REC>0001>100041>18000>TSOP>C>18000000046>DO
  23. REC>0001>100041>18000>TSOP>C>18000000046>DO
  24. REC>0001>100041>18000>TSOP>C>18000000046>DO
  25. REC>0002>000146>18000>TSOP>M>18000000046>DO
  26. REC>0001>000243>18000>TSOP>M>18000000046>DO
  27. REC>0001>000243>18000>TSOP>C>18000000046>DO
  28. REC>0001>000243>18000>TSOP>C>18000000046>DO
  29. REC>0001>000243>18000>TSOP>C>18000000046>DO
  30. REC>0001>000243>18000>TSOP>C>18000000046>DO
  31. REC>0003>100045>18000>TSOP>M>18000000046>DO
  32.  
Jan 25 '09 #7

P: 6
Thank you so much for your help. I am a beginner in Perl and that's why i was not able to figure that out.
I appreciate your effort
Jan 26 '09 #8

P: 6
Hi KevinADC
I really want to learn Perl well. Can you please explain the following script:
$myhash{$refNum}{'Total'} += $numpages;

I am very much thankful to you.
Jan 26 '09 #9

P: 6
Hi KevinADC,
I just figured out my previous question.Please do not respond.
Thank you
Jan 26 '09 #10

Post your reply

Sign in to post your reply or Sign up for a free account.