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


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Perl/Array Sorting : Can someone please explain below code $h{$_}++
# 1  
Old 03-15-2019
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 03-18-2019
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:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Perl: Can someone please explain this code "sort { $a <=> $b } @unsorted"

@sorted = sort { $a <=> $b } @unsorted; I am having hard time understanding how this works? I know the output but interested to know the working. Thanks in advance. (2 Replies)
Discussion started by: Tanu
2 Replies

2. Shell Programming and Scripting

can you explain this perl command?

I am using this line of perl code to change the file format and remove ^M at the end of each line in files: perl -i -pe's/\r$//;' <name of file here> Can you explain to me what this code does, and translate it into bash/awk/sed? (2 Replies)
Discussion started by: locoroco
2 Replies

3. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

4. Shell Programming and Scripting

[Perl] Sorting an String-Array

Hi, i have a txtfile with the format <Nr>tab<word>tab<other stuff>new line and i want to sort the <word>-colum with a perl script. My textfile: <Nr>tab<word>tab<other stuff>new line 6807 die ART.Acc.Sg.Fem 6426 der ART.Gen.Sg.Fem 2 die ART.Nom.Sg.Fem 87 auf APPR.-- 486 nicht PTKNEG.--... (1 Reply)
Discussion started by: buckelede
1 Replies

5. Shell Programming and Scripting

Can someone please explain what tr#A-Za-z0-9+/# -_#; means in perl?

Can someone please explain what tr#A-Za-z0-9+/# -_#; means in perl? (4 Replies)
Discussion started by: 3junior
4 Replies

6. Shell Programming and Scripting

perl array sorting

Hi All, I have an array in perl as @match = (201001,201002,201001,201002); I am trying to sort this array as @match = sort(@match); print "@match"; I dont see the output sorted any answers I also tried another way, but still the results are not sorted foreach my $match (sort { $a... (2 Replies)
Discussion started by: bsdeepu
2 Replies

7. Shell Programming and Scripting

Sorting value frequency within an array

How is it possible to sort different nummeric values within an Array. But i don`t want the highest or the lowest. I need the most frequently occurring value. For examble: My Array has to following values = (200 404 404 500 404 404 404 200 404) The result should be 404 The values are... (3 Replies)
Discussion started by: 2retti
3 Replies

8. Shell Programming and Scripting

perl array matching between area code and no.

Hi Everyone, I have: my @No=qw(032106 032630 0380 034010 035110 0354801111); my $str_No=join(';', @No); I have a string $strA="03263033", so in order to determine this $strA area code matches with @No, I can do: if ( (rindex($str_No,substr($strA,0,5))))== -1) ) { print "Not... (1 Reply)
Discussion started by: jimmy_y
1 Replies

9. UNIX for Advanced & Expert Users

perl explain syntax !!!

hi all i was going through some perl code i came across this line and i am not getting what is exactly going on .. $$this{localtion} = GetName->GetVarName("EXE_DIR") ; what is the red part doing in above code (2 Replies)
Discussion started by: zedex
2 Replies

10. Shell Programming and Scripting

Perl: Sorting an associative array

Hi, When using sort on an associative array: foreach $key (sort(keys(%opalfabet))){ $value = $opalfabet{$key}; $result .= $value; } How does it handle double values? It seems to me that it removes them, is that true? If so, is there a way to get... (2 Replies)
Discussion started by: tine
2 Replies
Login or Register to Ask a Question