perl - Understanding code: Hash, grep for duplicates (modified to check for multiple elements) -



perl - Understanding code: Hash, grep for duplicates (modified to check for multiple elements) -

code:

@all_matches = grep { ! ( $seensentence { $_->[0] .'-'. $_->[1] .'-'. $_->[5] } ++ ) } @all_matches;

purpose: code removes duplicates of elements array @all_matches aoa.

my effort @ total breakdown ( ??..?? around i'm unsure ):

grep returns elements of @all_matches homecoming true.

the key of hash %seensentence ??the 3 elements?? of @all_matches. since hash can have unique keys, first time through it's value incremented undef(0) 1. next time through, defined value, ! means grep returns if it's undef (unique value associated element).

my questions:

(1) how can turn {$_->[0] .'-'. $_->[1] .'-'. $_->[5]}++ hoh?

i told (idiomatic) way accomplish it. stab in dark be:

( {$_->[0] => 0, $_->[1] => 0, $_->[5] => 0} )++

(1b) because i don't understand how original doing want to. read -bareword equiv "-bareword" tried: {"$_->[0]" . "$_->[1]". "$_->[5]"} , seemed work exact same. still don't understand: treating each element key (a) separately (like array of keys) or (b)correct: simultaneously (since . concatenates them 1 string) or (c) not doing think is?

(2) mean: $_->[0] || $_->[1] || $_->[5] ? doesn't same above.

i read that: short circuit logical operators homecoming lastly value, check value @ {$_->[0]} , if there one, thought value there incremented, if not check next element until none true, when grep pass unique value on.

thanks time, tried thorough possible (to fault?) allow me know if there missing.

first lets turn grep foreach loop can examine more clearly. i'm going expand of idioms larger constructs clarity's sake.

my @all_matches = ( ... ); { %seen; @no_dupes; foreach $match ( @all_matches ) { $first_item = $match->[0]; $second_item = $match->[1]; $third_item = $match->[5]; $key = bring together '-', $first_item, $second_item, $third_item; if( not $seen{ $key }++ ) { force @no_dupes, $match; } } @all_matches = @no_dupes; }

in other words, original coder creating hash key using array reference held in $match, each of referent indices of $match->[0], 1, , 5. hash keys unique, duplicates dropped checking if key exists before pushing @no_dupes.

the grep{} mechanism more code-efficient (ie, quicker type, , no throwaway variables) idiom accomplish same thing. if works, why refactor it? not doing need improve upon?

to same hoh, this:

my @all_matches = ( ... ); { %seen; @no_dupes; foreach $match ( @all_matches ) { $first_item = $match->[0]; $second_item = $match->[1]; $third_item = $match->[5]; if( not $seen{ $first_item }->{ $second_item }->{ $third_item }++ ) { force @no_dupes, $match; } } @all_matches = @no_dupes; }

which translated grep follows:

my @all_matches = ( ... ); { %seen; @all_matches = grep { not $seen{$_->[0]}->{$_->[1]}{$_->[5]}++ } @all_matches; }

however, case don't see clear advantage building datastructure, unless intend utilize %seen later else.

with respect || operator, that's different animal. can't think of useful way employ in context. logical short circuit operator of, say, "$a || $b || $c" tests boolean truthfulness of $a. if it's true, returns value. if it's false, checks $b same way. if it's false, checks $c same way. if $a true, $b never gets checked. if $b true, $c never gets checked.

perl

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -