散列分配为数组

时间:2017-09-15 13:23:40

标签: perl

我试图理解下面的代码;我只是无法理解第15行正在做什么。

似乎它正在尝试初始化/分配给%heading,但我不确定该语法是如何工作的。

$strings = [qw(city state country language code   )];

my $file = "fname";
my $fn   = $strings;
my $c    = 0;

open( FILEH, "< ${file}.txt" ) or die( $! );

while ( <FILEH> ) {

    my %heading;
    chomp;
    $c++;

    @heading{ ( @$fn, "One" ) } = split( /[|]/ );  # Line 15

    if ( defined( $heading{"One"} ) ) {
        my $One = $heading{"One"};
    }

1 个答案:

答案 0 :(得分:6)

这被称为“切片”。它一次分配给几个键:

class StreamingData extends Serializable {
  def main(args: Array[String]) {
    val conf = new SparkConf().setAppName("Application").setMaster("local[2]")
    //val sc = new SparkContext(conf)
    val ssc = new StreamingContext(conf, Seconds(1))
    val input = ssc.textFileStream("file:///C:/Users/M1026352/Desktop/Spark/StreamInput")
    val lines = input.flatMap(_.split(" "))
    val words = lines.map(word => (word, 1))
    val counts = words.reduceByKey(_ + _)
    counts.print()
    ssc.start()
    ssc.awaitTermination()
  }

}

是一种更短更快的做法

@hash{ $key1, $key2 } = ($value1, $value2);

$hash{$key1} = $value1; $hash{$key2} = $value2; @$fn相同,即数组解除引用。