星期二, 五月 17, 2005

Perl 印出 hash 和 array

Perl 要印出 hash 的全部內容, 有以下幾種方法:

  1. while(my ($key, $val) = each(%hash)) { print "$key $val" }
  2. %hash = qw / car 600k bike 70k /; print $_ , $hash{$_} , "\n" foreach (keys %hash);
  3. print "$_ $hash{$_}\n" foreach (keys %hash);
  4. print "$_ $h{$_}\n" for keys %h;

Perl 印出 Array 的內容:

  1. print $_."\n" for @a;
  2. print @a;
  3. push @a, split for <>; print @a;
相關標籤

this is comment icon 字串置換 [回覆]

請問一下,因為小弟對perl的Hash還是不太熟悉,所以有個題目是這樣

撰寫一支名為test.pl 的程式,接受兩個參數。
第一個參數是字串檔名,第二個參數是文字檔名。
文字檔是普通的純文字檔案。
字串檔的格式是一行一筆,每筆有兩個欄位,以 tab 隔開。
第一個欄位是要置換的目標,第二個欄位是置換內容。
test.pl 會將原始文字檔中所有符合置換目標的地方取代為置換內容寫回

【執行範例】
假設有一個字串檔 test1.txt 內容是:

a apple
b big
g grape

而文字檔 test2.txt 內容是:

i eat a
very b
i like g

在執行 %test.pl test1.txt test2.txt 之後,
test2.txt 內容變為如下:

i eat apple
very big
i like grape

以上問題請多多指導小弟,如何把"字串檔"運用到"HASH"? 之後再如何去置換呢? 謝謝囉!

Comment by jkz (08/15/2008 13:00)

this is comment icon 回 jkz [回覆]

這應該是作業吧. @.@a..
如果我做的話, 取 text 1 的第一個字當 hash 的 key, 或 text 2 的最後一個字也來當 hash 的 key.
然後去相互對應一下, 只要 hash 的 key 都一樣(或者 hash key 有存在值, 就代表兩者是一樣的, 就可以合在一起~ :)

Comment by Tsung (08/15/2008 13:09)

this is comment icon [回覆]

請問有沒有比較實際的例子呢?
現在小弟想著要如何把test1.txt字串檔的內容如何放入hash當keys及values......想破頭了......是否要先把test1.txt的內容先存放在什麼地方再放入hash當中呢?

Comment by jkz (08/15/2008 13:18)

this is comment icon 回 jkz [回覆]

Mmm... 這個就要靠自己囉~
作業要自己做才學的到的東西~ :)

Comment by Tsung (08/15/2008 13:29)

this is comment icon 關於hash [回覆]

假如我是這樣寫呢? 有哪裡需要修改嗎?

open TEST,"< test1.txt";
while ($line = )
{
($i,$j) = split(//,$line);
$i = keys %hash;
$j = values %hash;
}
close TEST;

Comment by jkz (08/15/2008 13:45)

this is comment icon 回 jkz [回覆]

你應該先試著寫出來, 然後告訴我哪邊有問題.
而不是丟程式讓我幫你繼續把他寫完~ :)
試著把他寫完, 然後告訴我問題是出在不知道怎麼抓 hash? 還是不會拆字串? 等等.
這樣子才有意義囉~ :)

Comment by Tsung (08/15/2008 13:51)

this is comment icon 關於hash及字串置換 [回覆]

不好意思,小弟本人是有寫了,只是試很多次,還是無法將test1.txt字串檔的內容用hash去置換
test2.txt的內容,程式如下:
另外,小弟無意打擾多次,只是這個範例可以救目前在系統上的問題,而小弟對於perl又不太熟悉所以才會很急著請教,請多多包涵.....謝謝囉!! :)

open DIC," $source";
while ($line = )
{
$line =~ (s/$k/$v/);
print SOURCE $line;
}

close SOURCE;
close DIC;

Comment by jkz (08/15/2008 14:12)

this is comment icon 程式寫出來的太長 分開寫 part 1 [回覆]

--------------------------------part 1
open TEST,"< $test1.txt";
while ($line = )
{
($k,$v) = split(//,$line);
$k = keys %hash;
$v = values %hash;
}
close TEST;
--------------------------------接著part 2

Comment by jkz (08/15/2008 14:18)

this is comment icon 程式寫出來的太長 分開寫 part 1 [回覆]

--------------------------------part 1
open TEST,"< $test1.txt";
while ($line = )
{
($k,$v) = split(//,$line);
$k = keys %hash;
$v = values %hash;
}
close TEST;
--------------------------------接著part 2

Comment by jkz (08/15/2008 14:18)

this is comment icon 程式太長分開寫 PART 2 [回覆]

--------------------------------------part 2
open TEST1," test2.txt";
while ($line = )
{
($k,$v) = split(//,$line);
$k = keys %hash;
$v = values %hash;
$line =~ (s/$k/$v/);
print TEST2 $line;
}
close TEST1;
close TEST2;
--------------------------------------------end
以上兩個部份合起來的地方,怎麼試都沒辨法取代,請高手多多指點,謝謝囉!! :)

Comment by jkz (08/15/2008 14:22)

this is comment icon 程式太長分開寫 PART 2 [回覆]

--------------------------------------part 2
open TEST1," test2.txt";
while ($line = )
{
($k,$v) = split(//,$line);
$k = keys %hash;
$v = values %hash;
$line =~ (s/$k/$v/);
print TEST2 $line;
}
close TEST1;
close TEST2;
--------------------------------------------end
以上兩個部份合起來的地方,怎麼試都沒辨法取代,請高手多多指點,謝謝囉!! :)

Comment by jkz (08/15/2008 14:22)

this is comment icon 回 jkz [回覆]

把你的程式 Email 給我, 我再給你簡單點的範例吧.

Comment by Tsung (08/17/2008 10:30)

this is comment icon Mr. Tsung 您好, 這是我的解法, 想請教您是否有更短的簡潔的寫法呢? [回覆]

die "usage:argv ppattern error!! " unless $#ARGV==1;
#!/usr/bin/perl -w
use strict;
my ($dicfile,$file) = ($ARGV[0], $ARGV[1]);
my %hash;

if (-e $dicfile) {
my @keyvalue;
open DICT, $dicfile or die "$!";
while () {
chomp $_;
push @keyvalue, split(/\t/, $_);
}
%hash = @keyvalue;
}
close $dicfile;

if (-e $file) {
open STRFILE, $file or die "$!";
while () {
chomp $_;
my $strlen=length($_);
substr($_, $strlen-1, 1) = $hash{substr($_,$strlen-1,1)};
print "$_\n";
}
}
close $file;

Comment by wimlove (11/21/2008 19:40)

this is comment icon 不好意思 貼上來以後縮排全都亂了, 還請您不吝賜教 謝謝 [回覆]

無內文

Comment by wimlove (11/21/2008 19:43)

this is comment icon 回 wimlove [回覆]

#!/usr/bin/perl -w
# USAGE: ./file.pl test1 test2
$t1 = $ARGV[0];
$t2 = $ARGV[1];
%h1 = %h2 = ();

open T1,"< $t1.txt";
open T2,"< $t2.txt";

while ()
{
chomp $_;

@match = $_ =~ /(\w) (\w+)/;
$h1{$match[0]} = $match[1]; # $h1[a] = 'apple';
}

while ()
{
chomp $_;

@match = $_ =~ /(.*) (\w)$/;
$h2{$match[1]} = $match[0]; # $h2[a] = 'apple';
}

close T1;
close T2;

#while(my ($key, $val) = each(%h1)) {
# print "$key $val\n";
#}
#
#while(my ($key, $val) = each(%h2)) {
# print "$key $val\n";
#}

while(my ($key, $val) = each(%h2)) {
print $h2{$key} . " " . $h1{$key}. "\n";
}

Comment by Tsung (11/24/2008 00:20)

this is comment icon 回 wimlove [回覆]

Mmm... 您寫的我的不能跑, 這些是之前跟你說的方法, 寫法很簡單.
注意那個 while () 裡面是 "左中括號 T1 右中括號" 和 "左中括號 T2 右中括號", 因為留言會濾 Tag, 所以不見了. 參考看看吧.

Comment by Tsung (11/24/2008 00:23)
Add this page to del.icio.us Add this page to Yahoo Taiwan's bookmark Add this page to MyShare

發表迴響

標題

內容 (限制 1000 字)

暱稱

電子郵件

個人網頁


 authimage


PS: 若無法留言, 請先確認是否有打開 JavaScript, 造成您的困擾, 實在萬分對不起 Orz...(如果無法留言, 勞煩可以發信給我好嗎? 謝謝.)
PS2: 若您的留言被誤判, 我都會再自行看過, 不需要一直重覆張貼~