1
首先感謝您抽出寶貴時間看一下。Perl:使用foreach循環從哈希中打印前25個值
我是新來的perl,而且我在學習的每一門該死的語言中都與計數相抗爭......猜猜我小時候掉了太多次。我正在運行腳本grep安全日誌通過ssh登錄失敗。
我想只打印此代碼的前25個結果,但我所嘗試的所有內容似乎都沒有做到這一點(而另一個foreach):/希望得到一些想法,然後facepalm自己。這也是後期......是啊,就是這樣......這就是爲什麼我很愚蠢
# list each IP ($z) and number of attempts ($count{$z}, sort by highest amount of attempts
foreach $z (sort { $count{$b} <=> $count{$a} } (keys %count)) {
@geoData= &getGeo($z); # use subroutine getGeo to get location info
$cityName = $geoData[0]; # extract city from location info
$countryCode = $geoData[1]; # extract country code from location info
$reg = $geoData[2]; # extract country code digit
printf " %4d %-20s %-3s %-6s %-20s\n", $count{ $z }, $z, $countryCode, $reg, $cityName;
}
下面是輸出的將是什麼樣子,它只是列舉了數百IP的,我只是想25
=======================Intrusion Report=======================
Count Remote IP Address Country City
==============================================================
7427 182.x.x.x IN
4091 200.x.x.x DO 05 Santo Domingo
2808 42.x.x.x CN
2223 114.x.x.x CN 23 Shanghai
1958 122.x.x.x CN 02 Hangzhou
1915 202.x.x.x ID 04 Jakarta
1410 klinikk.x.x.x RU 47 Klin
1190 195.x.x.x DE 16 Berlin
805 60.x.x.x CN 01 Hefei
646 tango054.x.x.x DE
645 216.x.x.x US CA Canyon Country
645 72.x.x.x US CA Canyon Country
583 61.x.x.x CN 02 Wenzhou
預先感謝您!
哇,welp我沒有看到我的書。我習慣於按照i ++或其他任何方式來做某件事,但仍然在學習。謝謝你,謝謝你,謝謝你!!!!!!!!!!!!!!!等等,我很快就說過了,它不按預期打印 – thewrp
@thewrp:不客氣。不要忘記接受答案(它也會提升你的代表) – Zaid
它似乎從我的散列中隨機打印25個具體值,仍然以降序排列,但除此之外我看不到一個模式:( – thewrp