in reply to How do I count the number of occurrences of each string in an array?
... I'd like to understand why it's not working rather than just getting a quick fix to the problem.
It doesn't work because there is only (and can only ever be) a single element in the @data array. This is because join returns a single string from all the sub-strings it joins together. (Update: If there were no sub-strings to join together, e.g., if the match had failed, join would still return a single (empty) string.)
So in a sense your code does work: it loops through the single element in the array and determines that that element is, indeed, exactly equal to itself: one duplicate.c:\@Work\Perl\monks>perl -wMstrict -le "use Data::Dump qw(dd); ;; $_ = '2015-08-13 17:53:45 blah yada'; my @data = join('', $_ =~ /^(\d\d\d\d)-(\d\d)-(\d\d) (\d+):(\d+):(\d+ +)/); ;; print 'number of elements in array: ', scalar @data; ;; print qq{element at index $_: '$data[$_]'} for 0 .. $#data; ;; dd \@data; " number of elements in array: 1 element at index 0: '20150813175345' [20150813175345]
Update: So the take-away lesson is know your data. What was in the array to begin with? Tools like Data::Dumper and Data::Dump are life-savers when it comes to debugging questions like this for yourself. For a bright, confident outlook on life, dump early and dump often.
Give a man a fish: <%-(-(-(-<
|
|---|