Perl:如何在循环中访问带有数组值的哈希?

时间:2016-10-07 15:49:45

标签: perl

我已经将数组的哈希定义为值:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.abcd.rep.xyz</groupId>
        <artifactId>xyz</artifactId>
        <version>1.0</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
        <artifactId>SparkPOC</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
        <name>SparkPOCde</name>
        <url>http://maven.apache.org</url>
<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>


<spark-version>1.6.1</spark-version>

<kafka-version>0.9.0.0</kafka-version>

</properties>


<dependencies>

<!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->



<!-- http://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>

<groupId>log4j</groupId>

<artifactId>log4j</artifactId>

<version>1.2.17</version>

</dependency>

<!-- http://mvnrepository.com/artifact/org.apache.spark/spark-streaming_2.10 -->

<dependency>
<groupId>org.apache.spark</groupId>

<artifactId>spark-streaming_2.10</artifactId>

<version>${spark-version}</version>

</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-streaming-kafka_2.10 -->



<dependency>

<groupId>org.apache.spark</groupId>

<artifactId>spark-streaming-kafka_2.10</artifactId>

<version>1.6.2</version>

 <exclusions>
        <exclusion>
            <groupId>io.netty</groupId>
            <artifactId>netty</artifactId>
        </exclusion>

        <exclusion>
            <groupId>io.jboss.netty</groupId>
            <artifactId>netty</artifactId>
        </exclusion>
    </exclusions> 

</dependency>

   <dependency>
            <groupId>com.abcd.rep.xyz</groupId>
            <artifactId>xyzCommon</artifactId>
            <version>1.0</version>
            <type>jar</type>
        </dependency>


<!-- http://mvnrepository.com/artifact/ojdbc/ojdbc -->


<!-- <dependency> <groupId>ojdbc</groupId> <artifactId>ojdbc</artifactId> <version>14</version> </dependency>-->


<!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->




<!-- http://mvnrepository.com/artifact/org.springframework.data/spring-data-mongodb -->




<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->





</dependencies>


<build>

<finalName>appname</finalName>


<resources>


<resource>

<directory>src/main/resources</directory>


<excludes>

<exclude>eventRules.json</exclude>

<exclude>log4j.xml</exclude>

<exclude>resources.properties</exclude>

</excludes>

</resource>

</resources>


<plugins>


<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.5.1</version>


<configuration>

<source>1.8</source>

<target>1.8</target>

</configuration>

</plugin>


<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-shade-plugin</artifactId>

<version>2.4.3</version>


<executions>


<execution>

<phase>package</phase>


<goals>

<goal>shade</goal>

</goals>


<configuration>


<filters>


<filter>

<artifact>*:*</artifact>


-<excludes>

<exclude>META-INF/*.SF</exclude>

<exclude>META-INF/*.DSA</exclude>

<exclude>META-INF/*.RSA</exclude>

</excludes>

</filter>

</filters>


<transformers>


-<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">

<resource>reference.conf</resource>

</transformer>


-<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">

<mainClass>com.abcd.rep.xyz.SparkPOCde.EventConsumerServiceImpl</mainClass>

</transformer>

</transformers>

</configuration>

</execution>

</executions>

</plugin>

</plugins>

</build>

</project>

我们如何使用循环访问每个键的值?

基本上,我想做类似的事情:

%myhash = ( 'key1' => [ 'key1value1', 'key1value2' ],
            'key2' => [ 'key2value1', 'key2value2' ],
            .......
            'key100' => [ 'key100value1', 'key100value2' ] );

在单独的行中打印每个键的两个数组值。

我尝试在for / while循环中使用索引但没有成功。

提前感谢您的任何建议!

3 个答案:

答案 0 :(得分:2)

这是我的解决方案......只是为了好玩,甚至可能更简洁。不确定你是否可以进一步压缩它:

print join ' ', @$_, "\n" for @myhash{sort keys %myhash};

适用于%myhash中的任意长值数组。

编辑:

正如simbabque在评论中指出的那样,它确实可以进一步压缩。谢谢你的提示!

say join ' ', @$_ for @myhash{sort keys %myhash}; # requires: use feature 'say';

答案 1 :(得分:1)

以这种方式:

%myhash = ( 'key1' => [ 'key1value1', 'key1value2' ],
            'key2' => [ 'key2value1', 'key2value2' ],
            'key3' => [ 'key3value1', 'key3value2' ] );

for my $k (sort keys %myhash)
{
    print "$myhash{$k}->[0] $myhash{$k}->[1]\n";
}

如果数组元素的数量不同:

for my $k (sort keys %myhash)
{
    for my $v (@{$myhash{$k}})
    {
        print "$v ";
    }
    print "\n";
}

答案 2 :(得分:1)

另一种解决方案:

print map {  join(" ", @{ $myhash{$_} }, "\n") } sort keys %myhash;

(简洁的方式)