Guest User

Untitled

a guest
Jun 19th, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. use strict;
  2. use warnings;
  3.  
  4. # Column details and sample line, from the post
  5. my $header = q{0 AOZSVIN, TAMSSZ B A A- B+ B B- C+ C C- D+ D D- F CR P PR I I* W WP WF AU NR FN FS};
  6. my $sample = q{0 AAS 150 23 25 16 35 45 14 8 10 2 1 1 4 4 };
  7. # -+--------+-----+-----+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---..
  8. # chars 1212345678912345612345612341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234...
  9. # num. chars: 2 9 6 6 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 *
  10. my $unpack = q{A2A9 A6 A6 A4 A4 A4 A4 A4 A4 A4 A4 A4 A4 A4 A4 A4 A4 A4 A4 A4 A4 A4 A4 A4 A4 A4 A*};
  11. $unpack =~ s/\s//g;
  12.  
  13. # Get column names from the "$header" variable above
  14. my @column_names = unpack($unpack, $header);
  15. s/\s+$// for @column_names; # get rid of trailing spaces
  16. s/^\s+// for @column_names; # get rid of leading spaces
  17.  
  18. # Some sample data in same format, to try the script out
  19. my @samples = (
  20. q{0 AAS 150 23 25 16 35 45 14 8 10 2 1 1 4 4 },
  21. q{0 AAS 353 2 3 5 2 6 1 2 },
  22. q{0 T304 480M 3 10 8 8 2 3 2 1 1 1 },
  23. q{0 BIOS 206 3 14 5 11 9 8 4 8 3 1 1 6 7 },
  24. );
  25.  
  26. my @big_sample = (@samples) ;#x 200_000;
  27.  
  28. my @unpacked_data_as_arrayrefs;
  29. my @unpacked_data_as_hashrefs;
  30. my $begin = time;
  31. for my $line ( @big_sample ) {
  32. my @data = unpack($unpack,$line);
  33. s/\s+$// for @data; # get rid of trailing spaces
  34. s/^\s+// for @data; # get rid of leading spaces
  35. push @unpacked_data_as_arrayrefs, [@data]; # stop here if this is all you need
  36. ## below converts the data in a hash, based on the column names given
  37. #my %as_hash;
  38. #for ( 0..$#column_names ) {
  39. # $as_hash{ $column_names[$_] } = $data[$_];
  40. #}
  41. #push @unpacked_data_as_hashrefs, { %as_hash };
  42. }
  43. my $tot = time - $begin;
  44. print "Done in $tot seconds\n";
  45.  
  46. # verify all data is as we expected
  47. # uncomment the ones that test hashref, if the above hashref-building code is also uncommented.
  48. {
  49. use Test::More;
  50. # first sample
  51. is($unpacked_data_as_arrayrefs[0]->[2],'AAS'); # AAS in the third column
  52. is($unpacked_data_as_arrayrefs[0]->[7],'35'); # 35 in the 8th column
  53. # fourth sample
  54. is($unpacked_data_as_arrayrefs[3]->[2],'BIOS');
  55. is($unpacked_data_as_arrayrefs[3]->[15],'6');
  56. # sixth
  57. is($unpacked_data_as_arrayrefs[5]->[7],'114');
  58. is($unpacked_data_as_arrayrefs[5]->[10],'75');
  59. done_testing();
  60. }
Add Comment
Please, Sign In to add comment