[완료]perl array print 와 List::Compare 관련

ascendo의 이미지

안녕하세요

List::Compare 모듈을 이용하여

아래의 프로그램을 해보았는데

원하는 데로 결과가 나오질 않아 메일드립니다.

@ perl cluster_compare.pl cluster1.txt cluster2.txt

이렇게 실행시키면

결과가

cluster #### tab cluster#### xxxx tab xxxx tabxxxx tab .....

이렇게 나올줄 알았는데

cluster #### tab cluster#### xxxx enter xxxx enterxxxx
enter .....

이런식으로 나옵니다.

원하는 결과는 나오지만 출력 형식의 문제 가 있는데요

뭐가 잘못된건지 한번 부탁드겠습니다

제 생각으로는 array 를 프린팅할때의 문제 인것 같은데요

array 사이를 tab으로 프린팅 하는 방법도 부탁 드립니다.

#!/usr/bin/perl
#
use List::Compare;
use Inline::Files;
 
open DATA, "$ARGV[0]";
open DATA2, "$ARGV[1]";
 
 
%cluster;
 
 
while(<DATA>)
{	
	@line=split '\t',$_;
	#@{$cluster{$line[0]}}=@line[1];
	push @{$cluster{$line[0]}}, $line[1];
}
 
 
 
while(<DATA2>)
{
	@line=split ' ',$_;
#	$line[1]=~s/\W//g;
	$lc=List::Compare->new(\@{$cluster{$line[0]}},\@{$cluster{$line[1]}});
	@common=$lc->get_intersection;
	print "$line[0]\t$line[1]\t@common\n";
 
}

File attachments: 
첨부파일 크기
Plain text icon cluster1.txt574.6 KB
Plain text icon cluster2.txt27.38 KB
aero의 이미지

chomp 함수를 보세요.

ascendo의 이미지

좀 자세하게는 안될까용?

JEEN의 이미지

cluster1.txt cluster2.txt
파일들에 줄바꿈이 있을테니까... 그거 없애줘야 enter가 안들어가겠죠.
우선은...
Perl에 대한 실시간 답변을 듣고 싶으시면 irc.hanirc.org #perl에 오시면
친절하게 가르쳐 주실분들이 많을 겁니다. :-)
________
use perl;

use perl;

pung96의 이미지

파일에서 읽어들인 줄들이 줄바꿈문자(리눅스에서는 "\n")을 가지고 있기 때문에
split으로 나눠도 $line[1]의 제일뒤에 줄바꿈 문자가 남게 됩니다.
나중에 이 줄바꿈 문자가 출력되면서 요소마다 줄이 바뀌는 거지요.

while(<DATA1>){
    chomp;
    print;
}

위처럼 chomp를 하시면 줄바꿈 문자가 제거됩니다.

ascendo의 이미지

개행문자 덕분이었군요

덕분에 해결했습니다.

감사합니다.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.