Perl哈希排序问题 [英] Problem with Perl hash sorting

查看:100
本文介绍了Perl哈希排序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名新手perl程序员,我试图通过散列中的值对哈希进行排序,我遇到了问题。我以前做过这个,但这次出了问题,我不知所措。


我正在读一个带有while语句的大文件,每行都有一堆信息。我在空白处使用split,只保留我需要的信息。这里很容易。我要保留的是车站号码,车站名称,经度,纬度和车站年龄。这是代码的简化版本,但实际上我正在读取一个包含20列而不是4列的文件:

I am a novice perl programmer, and I am trying to sort a hash by a value in the hash and I am having problems. I''ve done this before, but this time something is going wrong and I''m at a loss.

I''m reading in a large file with a while statement, with each line having a bunch of information. I use split on the whitespace and keep only the information I need. Pretty easy through here. What I''m keeping is a station number, station name, longitude, latitude, and station age. This is a simplified version of the code, but in reality I''m reading in a file with 20 columns, not 4:

展开 | 选择 | Wrap | 行号

推荐答案

问候Dave,


首先,在哈希分配中,您为每个哈希赋予相同的值
Greetings Dave,

First off, in your hash assignments you are giving each hash the same value "


name",而不是它们的相应值。其次,您目前只按键进行排序,而不是按键对应的值。


无论如何,我建议您更改脚本的一些事项。但是,我只会告诉你,而不是建议你。

name", instead of their corresponding values. Secondly, you''re currently only sorting by the keys instead of the values corresponding to the keys.

Anyway, there are a few things that I would advise you to change about your script. However, instead of advising you, I''ll just show you.

展开 | 选择 | Wrap < span class =codeDivider> | 行号


谢谢!我试试看。我的代码通常非常难看。
Thanks! I''ll give this a try. My code is typically very ugly.


这篇关于Perl哈希排序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆