星期二, 五月 17, 2005
Perl 印出 hash 和 array
Perl 要印出 hash 的全部內容, 有以下幾種方法:
- while(my ($key, $val) = each(%hash)) { print "$key $val" }
- %hash = qw / car 600k bike 70k /; print $_ , $hash{$_} , "\n" foreach (keys %hash);
- print "$_ $hash{$_}\n" foreach (keys %hash);
- print "$_ $h{$_}\n" for keys %h;
Perl 印出 Array 的內容:
- print $_."\n" for @a;
- print @a;
- push @a, split for <>; print @a;
字串置換
請問一下,因為小弟對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"? 之後再如何去置換呢? 謝謝囉!
回 jkz
這應該是作業吧. @.@a..
如果我做的話, 取 text 1 的第一個字當 hash 的 key, 或 text 2 的最後一個字也來當 hash 的 key.
然後去相互對應一下, 只要 hash 的 key 都一樣(或者 hash key 有存在值, 就代表兩者是一樣的, 就可以合在一起~ :)
請問有沒有比較實際的例子呢?
現在小弟想著要如何把test1.txt字串檔的內容如何放入hash當keys及values......想破頭了......是否要先把test1.txt的內容先存放在什麼地方再放入hash當中呢?
關於hash
假如我是這樣寫呢? 有哪裡需要修改嗎?
open TEST,"< test1.txt";
while ($line = )
{
($i,$j) = split(//,$line);
$i = keys %hash;
$j = values %hash;
}
close TEST;
回 jkz
你應該先試著寫出來, 然後告訴我哪邊有問題.
而不是丟程式讓我幫你繼續把他寫完~ :)
試著把他寫完, 然後告訴我問題是出在不知道怎麼抓 hash? 還是不會拆字串? 等等.
這樣子才有意義囉~ :)
關於hash及字串置換
不好意思,小弟本人是有寫了,只是試很多次,還是無法將test1.txt字串檔的內容用hash去置換
test2.txt的內容,程式如下:
另外,小弟無意打擾多次,只是這個範例可以救目前在系統上的問題,而小弟對於perl又不太熟悉所以才會很急著請教,請多多包涵.....謝謝囉!! :)
open DIC," $source";
while ($line = )
{
$line =~ (s/$k/$v/);
print SOURCE $line;
}
close SOURCE;
close DIC;
程式寫出來的太長 分開寫 part 1
--------------------------------part 1
open TEST,"< $test1.txt";
while ($line = )
{
($k,$v) = split(//,$line);
$k = keys %hash;
$v = values %hash;
}
close TEST;
--------------------------------接著part 2
程式寫出來的太長 分開寫 part 1
--------------------------------part 1
open TEST,"< $test1.txt";
while ($line = )
{
($k,$v) = split(//,$line);
$k = keys %hash;
$v = values %hash;
}
close TEST;
--------------------------------接著part 2
程式太長分開寫 PART 2
--------------------------------------part 2
open TEST1,"
while ($line = )
{
($k,$v) = split(//,$line);
$k = keys %hash;
$v = values %hash;
$line =~ (s/$k/$v/);
print TEST2 $line;
}
close TEST1;
close TEST2;
--------------------------------------------end
以上兩個部份合起來的地方,怎麼試都沒辨法取代,請高手多多指點,謝謝囉!! :)
程式太長分開寫 PART 2
--------------------------------------part 2
open TEST1,"
while ($line = )
{
($k,$v) = split(//,$line);
$k = keys %hash;
$v = values %hash;
$line =~ (s/$k/$v/);
print TEST2 $line;
}
close TEST1;
close TEST2;
--------------------------------------------end
以上兩個部份合起來的地方,怎麼試都沒辨法取代,請高手多多指點,謝謝囉!! :)
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;
不好意思 貼上來以後縮排全都亂了, 還請您不吝賜教 謝謝
無內文
回 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";
}
回 wimlove
Mmm... 您寫的我的不能跑, 這些是之前跟你說的方法, 寫法很簡單.
注意那個 while () 裡面是 "左中括號 T1 右中括號" 和 "左中括號 T2 右中括號", 因為留言會濾 Tag, 所以不見了. 參考看看吧.
發表迴響
PS2: 若您的留言被誤判, 我都會再自行看過, 不需要一直重覆張貼~





