Store and read hash and array in files in Perl

前端 未结 4 763
眼角桃花
眼角桃花 2020-11-29 10:01

I\'m a noob.I need some basic knowledge about how data should be saved and read under perl. Say to save a hash and an array. What format (extension) of file should be used?

4条回答
  •  借酒劲吻你
    2020-11-29 10:27

    This really depends upon how you'd like store your data in your file. I will try writing some basic perl code to enable you to read a file into an array and or write back a hash into a file.

    #Load a file into a hash.
    #My Text file has the following format.
    #field1=value1
    #field2=value2  
    # is an opens a sample txt file in read-only mode.
    my %hash;
    while ()
    {
      chomp;
      my ($key, $val) = split /=/;
      $hash{$key} .= exists $hash{$key} ? ",$val" : $val;
    }
    

提交回复
热议问题