[perl] File에서 입력 받은 배열에 sort함수가 작동하지 않는 경우

manabooks의 이미지

txt파일에서 입력받은 배열에 sort함수가 작동하지 않아서 여쭙습니다.
(slackware 14.0 64bit리눅스를 사용하고 있습니다.)

아래와 같은 txt파일이 있습니다.
=== text.txt ===
1949.4.9. 4281민상 제197
2006.5.12. 2005다75910
2007.10.25. 2006다44791
2008.9.25. 2006다62492
2010.9.30. 2007다74775
2010.6.10. 2009다101275
2010.4.29. 2009다96083
2011.5.26. 2011다1330
2011.6.9. 2011다29307
1964.9.22. 63다743
1969.5.27. 68다725*
1995.10.13. 94다55385
================
이 파일을 정렬하고 싶어서 다음과 같은 perl code를 작성했습니다.

use strict ;
use warnings ;
 
open my $file, "<", $ARGV[0]  ;
my @array;
while (1){ 
		my $line = readline $file ; 
		last unless defined $line; 
		push @array , $line; 
}
 
sort @array ; 
for (my $i = 0 ; $i < scalar(@array) ; $i ++){
				print $array[$i]; 
}

그런대 화면에 출력된 결과에는 sort가 이루어지지 않았습니다.
다음과 같은 메세지가 함께 출력되었습니다.
 Useless use of sort in void context
시험삼아 아래와 같은 code를 perl로 돌려보았습니다.

my @array7 = ("1949.4.9. 	4281민상 제197", "2006.5.12. 	 2005다75910", "2007.10.25. 	 2006다44791", "2008.9.25. 	 2006다62492", "2010.9.30. 	 2007다74775", "2010.6.10. 	 2009다101275", "2010.4.29. 	 2009다96083", "2011.5.26. 	 2011다1330", "2011.6.9. 	 2011다29307", "1964.9.22. 	 63다743", "1969.5.27. 	 68다725*", "1995.10.13. 	 94다55")  ;
print join "\n", sort @array7;
print "\n" ;

이것은 제대로 작동했습니다.
1949.4.9. 4281민상 제197
1964.9.22. 63다743
1969.5.27. 68다725*
1995.10.13. 94다55
2006.5.12. 2005다75910
2007.10.25. 2006다44791
2008.9.25. 2006다62492
2010.4.29. 2009다96083
2010.6.10. 2009다101275
2010.9.30. 2007다74775
2011.5.26. 2011다1330
2011.6.9. 2011다29307

무엇이 문제인지 몰라서 도움을 부탁드립니다.

File attachments: 
첨부파일 크기
Plain text icon text.txt307바이트
익명 사용자의 이미지

sort @array 하면 @array를 변경하는게 아니고 @array를 소트해서 리턴하는 겁니다.
그래서 @array = sort @array 해야 소트된 것들이 @array에 들어갑니다.

위 소스를 제대로 수정하고 좀 더 perlish 하게 바꾸면

#/usr/bin/env perl
use strict ;
use warnings ;
 
open my $fh, '<', $ARGV[0];
my @array;
 
while (my $line = readline $fh) {
    chomp $line;
    push @array , $line;
}
 
@array = sort @array ;
 
foreach my $line (@array) {
    print "$line\n";
}

익명 사용자의 이미지

원라이너로 하신다면 아래와 같이 하실수도 있습니다.

perl -e 'print sort <>' filename

manabooks의 이미지

제가 잊고 있었네요.
정말 감사드립니다.
같은 컴퓨터에서 perl을 이용한 sorting이 처음이 아닌데, 작동하지 않는다고 당황했습니다.
더군다나 이유를 모르겠다고 생각하니, 기회가 되는데로 예전의 perl code도 다 고쳐야 하는 줄 알고 당황했습니다.
지적해주신 사항을 보니, 늘 새로운 배열을 선언해서 sort한 배열을 받아서 사용한 것이 바로 기억이 났습니다. (부끄럽네요.)

급한데로, bash 스크립트를 이용해서 sort와 sed, uniq를 조합해서 필요한 기능을 perl 밖에서 구현해서 사용하고 있었습니다.
덕분에 잘 해결했네요. perl로 모두 구현할 수 있게 되었습니다.

주로 사용하는 언어가 C였고, 문자열 처리는 sed, cut, paste, sort, uniq를 이용해와서 아직 perl에 서툰 점이 많습니다.
문자열 처리를 perl로 완전히 작성하기 시작한 것은 최근의 일입니다.

이런 초보적인 실수에 친절하게 답변해 주셔서 깊이 감사드립니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.