如何将数组和哈希传递给子例程 [英] How to pass an array and hash to a subroutine

查看:67
本文介绍了如何将数组和哈希传递给子例程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在处理一个脚本,该脚本接收某个文件A并将其与其他几个文件进行比较并打印A中存在的任何行但不是在其他文件中。我能够编写脚本代码,以便将文件A与其他4个文件进行比较。


但是,为了提高代码可读性并将文件A与可变数量的文件进行比较,我正在尝试使实际的比较过程成为一个子程序,以便可以调用它所需的次数。


所以我的代码接收第一个文件并读入每一行,然后生成一个哈希以行作为键及其值1.然后逐个接收每个其他文件并从文件中读取行。读取的行在条件语句中用于查看该键是否存在,如果存在,则将该键的值递增1.为了仅打印文件A的差异,我只打印哈希值为1的键。


我的代码的问题是虽然数组和散列被传递给子程序compare,但是当检查密钥是否存在时,由于某种原因,该语句总是为假。我认为问题与将参数传递到子例程有关,因为虽然我可以在子例程中成功访问数组和哈希,但条件语句永远不会满足。


所以,我不确定它是否是perl语法错误或程序员,但是对此的任何帮助都将非常感激。


Hi everyone,

I''m working on a script that takes in a certain file A and compares it against several other files and prints any lines that exist in A but not in the other files. I was able to code a script so that File A is compared against 4 other files.

However, to improve code readability and compare File A against a variable number of files, I am trying to make the actual comparison process a subroutine so that it can be called the necessary number of times.

So my code takes in the first file and reads in every line, and makes a hash with the line as the key and its value 1. It then takes in each of the other files one by one and reads in the line from the file. The line that''s read is used in a conditional statement to see if that key exists, and if it does, increments the value of that key by 1. In order to print only the differences of file A, I only print the hash keys that have values of 1.

The problem with my code is that although the array and hash is passed to the subroutine "compare," when the check to see if the key exists, the statement is always false for some reason. I believe the problem has something to do with the passing of the arguments into the subroutine because although I can access the array and hash successfully in the subroutine, the conditional statement is never satisfied.

So, I''m not sure if it''s a perl syntax error or programmer but any help on this would be greatly appreciated.


展开 | 选择 | Wrap | 行号

推荐答案

自从切换到PHP以来,我没有永远使用Perl,但你似乎没有回归你需要返回任何值。你需要返回值并对其进行评估或在子程序中返回TRUE或FALSE
I havn''t worked with Perl in forever since switching to PHP but you appear not be returning any value at all.You need to return the value and evaluate it or return TRUE or FALSE in your subroutine




那么条件语句就行了20:

if(exists(
Hi,
Well the conditional statement in line 20:
if (exists(


var {


这篇关于如何将数组和哈希传递给子例程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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