방문자
Scientist. Husband. Daddy. --- TOLLE. LEGE
[1] 이 블로그는 대한민국 저작권법(28조)과 U.S. Copyright Act (17 USC. §107)에 정의된 "저작권물의 공정한 이용원칙 | the U.S. fair use doctrine" 을 따릅니다. [2] 저작권(© 최광민)이 명시된 모든 글과 번역문들에 대해 (1) 원글의 URL 주소링크를 밝히지 않은 모든 형태의 (2) 전문 복제-배포, (3) 임의수정 및 자의적 본문 발췌, (4) 화면캡처 및 배포를 금지합니다. [3] 아울러 이 블로그의 내용을 AI 학습용으로 이용하는 것 역시 금지합니다. [운영] [대문으로] [방명록]
블로그 후원하기
[한국] 하나은행 (최광민): 376-910-500-183-07
[미국] 벤모 Venmo: @pay4kc
[국제] 페이팔 PayPal: @pay4kc
블로그 내부검색
주제
연재
주간 베스트
- [© 최광민] 천주, 상제, 하늘님, 하느님, 하나님
- [© 최광민] 예수 vs. 붓다 #8: 붓다는 어쩌다 예수가 되었을까?
- [© 최광민] 예수 vs. 미트라/미트라스 #01: 미트라스의 처녀탄생과 양치기의 경배?
- [© 최광민] 예수 vs. 붓다 #1: 소위 "불교계통" 복음서들의 정체: {이사전}, {보병궁 복음서}, {유란시아書}, {임마누엘의 탈무드}
- [© 최광민] 미국 전쟁부 (펜타곤) UFO/UAP 정보공개 현황
- [© 최광민] 예수 vs. 예수 #07: 삼위일체 개념은 어디서 유래했을까?
- [© 최광민] 나의 심우주 안시/영상관측용 NGC 천체 대상 55선 - 스텔라리움 투어
- [© 최광민] 예수 vs. 붓다 #5: 예수와 기독교 교부들은 윤회와 환생을 가르쳤을까?
- [© 최광민] 나는 외계인이다 - Je suis un E.T.
- [© 최광민] 예수 vs. 예수 #02: 예수는 언제부터 신이었을까?
최신 포스팅
Perl: my cookbook
Labels:
Informatics
이메일로 전송BlogThis!X에 공유Facebook에서 공유
Table of Content
- Installation
- Sort and get unique array elements
## Installation
perl -MCPAN -e 'install HTML::Template'
## Sort and get unuq array elements
SOURCE: http://docstore.mik.ua/orelly/perl/cookbook/ch04_07.htm
Straightforward: The question at the heart of the matter is "Have I seen this element before?" Hashes are ideally suited to such lookups. The first technique ( "Straightforward ") builds up the array of unique values as we go along, using a hash to record whether something is already in the array.
%seen = ();
@uniq = ();
foreach $item (@list) {
unless ($seen{$item}) {
# if we get here, we have not seen it before
$seen{$item} = 1;
push(@uniq, $item);
}
}
Faster: The second technique ( "Faster ") is the most natural way to write this sort of thing in Perl. It creates a new entry in the hash every time it sees an element that hasn't been seen before, using the ++ operator. This has the side effect of making the hash record the number of times the element was seen. This time we only use the hash for its property of working like a set.
%seen = ();
foreach $item (@list) {
push(@uniq, $item) unless $seen{$item}++;
}
The third example ( "Similar but with user function ") is similar to the second but rather than storing the item away, we call some user-defined function with that item as its argument. If that's all we're doing, keeping a spare array of those unique values is unnecessary.
%seen = ();
foreach $item (@list) {
some_func($item) unless $seen{$item}++;
}
Faster but different: The next mechanism ( "Faster but different ") waits until it's done processing the list to extract the unique keys from the %seen hash. This may be convenient, but the original order has been lost.
%seen = ();
foreach $item (@list) {
$seen{$item}++;
}
@uniq = keys %seen;
Faster and even more different: The final approach, ( "Faster and even more different ") merges the construction of the %seen hash with the extraction of unique elements. This preserves the original order of elements.
%seen = ();
@uniqu = grep { ! $seen{$_} ++ } @list;
이메일로 전송BlogThis!X에 공유Facebook에서 공유
Labels:
Informatics
- Installation
- Sort and get unique array elements
perl -MCPAN -e 'install HTML::Template' Scientist. Husband. Daddy. --- TOLLE. LEGE
[1] This blog complies with the "Fair Use Doctrine" as defined in Article 28 of the Republic of Korea Copyright Act and the U.S. Copyright Act (17 USC. §107). [2] Regarding copyrighted (© Kwangmin Choi) articles and translations, the following actions are prohibited (1) without providing a link to the original URL: (2) Full reproduction and distribution, (3) Unauthorized modification and arbitrary excerpting, and (4) Screen capturing and distribution. [3] Additionally, using the content of this blog for AI training is strictly prohibited. [운영] [대문으로] [방명록]
블로그 후원하기
[한국] 하나은행 (최광민): 376-910-500-183-07
[미국] 벤모 Venmo: @pay4kc
[국제] 페이팔 PayPal: @pay4kc


