Perl/Array Sorting : Can someone please explain below code $h{$_}++


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 6 Days Ago
Perl/Array Sorting : Can someone please explain below code $h{$_}++

Code:
sub uniq {
    my %h;
    return grep { !$h{$_}++ } @_
}

The above code is to remove duplicates from array.

I am having hard time understanding below things (basically around highlighted code in bold)-

when was the value inserted in hash?
and are we only adding a key in Hash not values?
Code:
$h{$_}++

what "++" means here
# 2  
Old 3 Days Ago
Basically
Code:
$h{$_}++

returns the value of $h{$_} then increments it. If there is no value at $h{$_} perl will initialise it to zero.

So, for each value that $_ may be, the first evaluation is zero and subsequent evaluations are non-zero. The exclamation point negates the values into True if zero, False if non-zero.

Does that help?

Andrew
These 2 Users Gave Thanks to apmcd47 For This Post:
Corona688 (2 Days Ago) Neo (3 Days Ago)
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Perl: Can someone please explain this code "sort { $a <=> $b } @unsorted" Tanu UNIX for Beginners Questions & Answers 2 6 Days Ago 12:09 PM
Sorting output of AWK array Jotne Shell Programming and Scripting 2 10-19-2012 04:30 AM
can you explain this perl command? locoroco Shell Programming and Scripting 2 12-24-2011 10:25 AM
HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl newbie_01 Shell Programming and Scripting 3 11-06-2011 01:26 AM
Sorting awk array output? Cowardly Shell Programming and Scripting 21 08-15-2011 11:06 AM
[Perl] Sorting an String-Array buckelede Shell Programming and Scripting 1 11-26-2010 07:20 PM
Can someone please explain what tr#A-Za-z0-9+/# -_#; means in perl? 3junior Shell Programming and Scripting 4 10-28-2010 12:18 PM
sorting numeric array phoeberunner Shell Programming and Scripting 1 08-05-2010 01:59 PM
perl array sorting bsdeepu Shell Programming and Scripting 2 02-08-2010 02:29 PM
Sorting value frequency within an array 2retti Shell Programming and Scripting 3 12-09-2009 05:08 AM
perl array matching between area code and no. jimmy_y Shell Programming and Scripting 1 08-05-2009 01:01 PM
perl explain syntax !!! zedex UNIX for Advanced & Expert Users 2 05-12-2009 11:02 AM
Sorting Awk generalized array gio001 Shell Programming and Scripting 2 02-21-2009 08:59 AM
sorting data using array in ksh ali560045 Shell Programming and Scripting 4 12-04-2007 03:26 AM
Perl: Sorting an associative array tine Shell Programming and Scripting 2 10-29-2003 03:11 AM