Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Adding text file data to hashes and array

by thanos1983 (Parson)
on Feb 13, 2019 at 15:38 UTC ( [id://1229875]=note: print w/replies, xml ) Need Help??


in reply to Adding text file data to hashes and array

Hello Tigor,

I was under the impression that you want also to calculate the average or the values e.g. (see bellow):

#!/usr/bin/perl use strict; use warnings; use IO::All; use Data::Dumper; use List::Util qw(sum); sub mean { return sum(@_)/@_; } my @lines = io('in.txt')->chomp->slurp; splice @lines, 0, 1; # remove first line my %hash; foreach my $line (@lines) { $line =~ s/^\s+//; my @elements = split /\s+/, $line; my $reference = splice @elements, 0, 1; $hash{$reference} = mean(@elements); } print Dumper \%hash; __END__ $ perl test.pl $VAR1 = { 'Pineapple' => '25', 'Apple' => '47.25', 'orange' => '22.5' };

If not you can simply do this (see bellow):

#!/usr/bin/perl use strict; use warnings; use IO::All; use Data::Dumper; my @lines = io('in.txt')->chomp->slurp; splice @lines, 0, 1; # remove first line my %hash; foreach my $line (@lines) { $line =~ s/^\s+//; my @elements = split /\s+/, $line; $hash{splice @elements, 0, 1} = \@elements; } print Dumper \%hash; __END__ $ perl test.pl $VAR1 = { 'Pineapple' => [ '10', '20', '30', '40' ], 'orange' => [ '12', '25', '24', '29' ], 'Apple' => [ '40', '45', '50', '54' ] };

I hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re^2: Adding text file data to hashes and array
by haukex (Archbishop) on Feb 13, 2019 at 18:46 UTC

    Two nits: Why use splice @lines, 0, 1; instead of shift, and why use split /\s+/ when split ' ' does the same thing but discards leading whitespace? splice @elements, 0, 1; discards the first element regardless of what it contains, even if the file format changes and there is no more whitespace at the front of the lines.

      Hello haukex,

      Yes you are right, I was not thinking clearly on that day and I made the code more complex without any reason :).

      Thanks for pointing out minor possible improvements.

      BR / Thanos

      Seeking for Perl wisdom...on the process of learning...not there...yet!
Re^2: Adding text file data to hashes and array
by Tigor (Novice) on Feb 14, 2019 at 06:51 UTC

    Hi thanos1983,The code which was shared by you absolutely brilliant piece of code and helped me a lot but what if there is a bit change in the text file mentioned below input file.I have tried but i am getting the output as {''=>[],Apple =>[A,40,45,50,54]} with first line empty and actual data is starting after the comma seperation & A in displaying in an array actually it has to dispay as {Apple-A =>[40,45,50,54]} .Any help is appreciated

    Below is the code used to achieve the output

    use strict; use warnings; use IO::All; use Data::Dumper; my @elements =(); my @lines = io('test_scores.txt')->chomp->slurp; splice @lines, 0, 1; # remove first line my %hash; foreach my $line (@lines) { $line =~ s/^\s+//; my @elements = split /\s+/, $line; $hash{splice @elements, 0, 1} = \@elements; } print Dumper \%hash;

    below is the Input file

    fruit Jan feb mar apr Apple A 40 45 50 54 orange O 12 25 24 29 Pineapple P 10 20 30 40

    output should be as below

    {Apple-A =>[40,45,50,54]}

      Make the key by joining the first 2 fields. see splice and join

      foreach my $line (@lines) { $line =~ s/^\s+//; my @elements = split /\s+/, $line; my $key = join '-',splice @elements, 0, 2; # 2 col key $hash{$key} = \@elements if length($key) > 0; # no blank keys }
      poj

        Hi Poj your code has worked for me,thank you so much but actually in the output i am gettingvar$1={'apple-A'=>['30','40','50','60']}

        How to get rid of the single quotes for the values 30,40,50,60

        input file data

        name jan feb mar apr apple 30 40 50 60 orange 25 54 45 45

        Please find the code below

        #!/usr/bin/perl use strict; use warnings; use IO::All; use Data::Dumper; my @lines = io('test_Scores.txt')->chomp->slurp; splice @lines, 0, 1; # remove first line my %hash; foreach my $line (@lines) { $line =~ s/^\s+//; my @elements = split /\s+/, $line; my $key = join '-',splice @elements, 0, 2; $hash{$key} = \@elements if length($key) > 0; } print Dumper \%hash;

      The code you posted in the root node works for this case...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1229875]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-19 13:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found