Re: [問題] 關於雜湊或是陣列的賦值

看板Perl作者 (李崩子)時間12年前 (2011/09/10 21:11), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
※ 引述《skylock (天鎖)》之銘言: : 最近在寫一段篩選字串的演算法 : 有一段是想要把字串拆開成單一的字母放到陣列裡面 : 然後計算每個單一字母在原字串中出現的次數 : 我把拆開的字母跟出現次數這兩個塞到變數裡面 : 再用迴圈寫成雜湊或是陣列中元素的資料格式 : 我測試過印出用迴圈跑出來的資料格式應該是正確的 : 可是再把那個資料塞到一個新的陣列或是雜湊裡面 : 就失敗了 : 因為印不出東西 : 感覺像是沒有放進去 : 請各位高手幫我看看 : 謝謝>< : 以下是我的程式碼 use Switch; #Convert the input into a LIST with each word (or character) as a list element. @mergelist = (''); $string = "BACDEAFCDGBACD"; $threshold = 1; $seperator = 'x'; #$t = 0; my @list = split(//, $string); push @list, $seperator; for (my $i=0; $i < $#list; $i++){ @match = ($string =~ /$list[$i]/g); $t = scalar @match; $data .= $list[$i].", ".$t.", "; } @match = ($string =~ /$list[$#list]/g); $t = scalar @match; $data .= $list[$#list].", ".$t; #print $data; => 這行印出來是正常的 my @freq = qw($data); ^^^^ qw 裡面是元素,所以你現在@freq 裡面 就只有$data這個元素 你 print @freq 就知道了,所以@freq 這個陣列只有 $data這個字串 你印第4個當然印不出來 我不知道你想要啥結果,不過可以參考看看XD my @freq = split/,/,$data; print $freq[3]; # 會印出 3 -- \ ∩──、 ===== \/ ● , \ ====== /\( ● ¯● |つ | ╳_入_ノ ミ ︵ 用這種餌 (_/ ノ / / 也想讓本大爺上鉤… /\___ノ_/ / ====== -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.116.215.174 ※ 編輯: sqrt1089 來自: 140.116.215.174 (09/10 21:15)
文章代碼(AID): #1EQs7pHw (Perl)
文章代碼(AID): #1EQs7pHw (Perl)