I disagree – hashes are not weird – they are incredibly useful. It is just an array indexed by a word instead of a number ☺
Here is some working code that may help === #!/usr/bin/perl use strict; use warnings; my @array = ( qw/ 11_ 22_ 33_ 33_ 33_ 44_ 44_ 55_ / ); my %results; # create a hash of arrays for my $item (@array) { push(@{ $results{$item} }, $item); } # For each entry in the hash for my $key (sort(keys(%results))) { # make a local array back out of what is stored in the hash my @arr = @{ $results{$key}}; print "key=$key:", $/; # print out the indexes of items in the local array print " $key$_",$/ foreach (0 .. $#arr) } Duncs From: James Kerwin [mailto:jkerwin2...@gmail.com] Sent: 09 February 2016 14:47 Cc: beginners@perl.org Subject: Re: Counter Help Thank you both very much for your help. I'll investigate this way when I get home later (I'm a bit wary of hashes because they're weird). As my files are sorted numerically I managed to do the following (it's ugly, please don't shout at me): my $length = (scalar @New)-1; #print $length; push (my @other, @New[0]); foreach (@New){ chop ($_); #print "$_\n"; } my $i = 0; my $ii = 1; foreach (@New[1 .. $length]) { #foreach (@New){ if ($_ ne $New[$i]){ push (@other, (join "", $_,"1")); $i++; } elsif ($_ eq $New[$i]){ $i++; $ii++; push (@other, (join "", $_,$ii)); }} foreach (@other){ print "$_\n"; } This gave me the desired output. Like I said, I'll investigate the other way later... Thanks, James. On Tue, Feb 9, 2016 at 2:22 PM, Jim Gibson <j...@gibson.org> wrote: > On Feb 9, 2016, at 6:08 AM, James Kerwin <jkerwin2...@gmail.com> wrote: > > Afternoon all, > > I have the following problem: > > I have an array containing a list of non-unique strings. eg: > > @array Contains: > > 111111_ > 222222_ > 333333_ > 333333_ > 333333_ > 444444_ > 444444_ > 555555_ > > What I would like to do is number each element of the array to look as > follows: > > 111111_1 > 222222_1 > 333333_1 > 333333_2 > 333333_3 > 444444_1 > 444444_2 > 555555_1 > > I have so far tried to use "exists", while loops and all the usual stuff as > it seemed very simple at first. My attempts have mostly focused on equating > the current element of the array with the previous one and asking if they are > equal and then taking action. I just can't get the logic right though. I keep > getting them all numbered sequentially from 1 to 10 or they all end in "_2" > or alternating "_1" and "_2" If anybody could shed even the smallest glimmer > of light on how to solve this I'd be really grateful. You should define a hash with the strings as keys and the number of strings encountered in the array so far as the hash value. You can then add the number to each string as you iterate over the array and increment the hash value count. Jim Gibson j...@gibson.org -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/