perl - How to put an array in hash of arrays without using its reference -


i using perl programming. stuck in situation there loop intakes file , splits array named data i.e @data. , have hash %hash adding array elements directly without reference.

in case while loop, $key holds single @data info in memory , puts of lines called after it. kindly provide perfect solutions.

while (loop in line line of file been readed) {     @data= split (/\|/, $line, -1);     %hash{$key}= \@data; } 

#! /usr/bin/env perl use common::sense; use yaml 'dump';  %results;  sub bad {   @data;   while (<data>) {     chomp;     @data = split /\|/, $_, 2;     $results{bad}{$data[0]} = \@data;   } }  sub {   while (<data>) {     chomp;     @data = split /\|/, $_, 2;     $results{good}{$data[0]} = \@data;   } }  sub good_also {   while (<data>) {     chomp;     /^([^|]+)/;  # pretend we're getting key other way     $results{good_also}{$1} = [split /[|]/, $_, 2]   } }  $data_pos = tell data; bad; seek data, $data_pos, 0; good; seek data, $data_pos, 0; good_also;  print dump(\%results);  "bad purchase: ", join '|', @{$results{bad}{purchase}}; "bad location: ", join '|', @{$results{bad}{location}}; "bad when: ", join '|', @{$results{bad}{when}};  __data__ purchase|apples location|fiesta when|today 

output:

--- bad:   location: &1     - when     - today   purchase: *1   when: *1 good:   location:     - location     - fiesta   purchase:     - purchase     - apples   when:     - when     - today good_also:   location:     - location     - fiesta   purchase:     - purchase     - apples   when:     - when     - today bad purchase: when|today bad location: when|today bad when: when|today 

Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -