Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

hash of hash

by Tigor (Novice)
on Feb 15, 2019 at 12:32 UTC ( [id://1229953]=perlquestion: print w/replies, xml ) Need Help??

Tigor has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: hash of hash
by Eily (Monsignor) on Feb 15, 2019 at 14:31 UTC

    At this point it's pretty obvious that this is just a series of exercises for which you are asking for an answer. Beyond the fact that perlmonks is not a code writing service (you can't rely on being given a full answer, because perlmonks is more about giving means and corrections, not the full solution), this means that there's a chance that you are expected to explore or demonstrate a specific tool or idiom of perl. But (one of) perl's motto is "There is more than one way to do it", so even if you get *a* solution, it might not be the expected one, and you probably won't have learned what you were meant to learn.

    For example in this case, this might be an exercise meant to demonstrate the use of slices, or split, or while with last... (there are plenty of other tools that could be used, but these are the most straightforward for your problem IMHO). It might even be about oneliners, in which case there's a very high chance that you'll just be given a solution that's completely out of topic.

Re: hash of hash
by choroba (Cardinal) on Feb 15, 2019 at 13:12 UTC
    What have you tried? How did it fail?

    How is this question different to the one you asked before? Can't you adapt the answers given there to this one?

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      hi i have achieved the desired output by using the following code.I tried to store the output in a variable and get average out of it but could not.Can some one help me out

      #!/usr/bin/perl use strict; use warnings; use IO::All; #use Data::Dumper; my @lines = io('Salesofcar.txt')->chomp->slurp; splice @lines, 0, 1; # remove first line my %hash; foreach my $line (@lines) { $line =~ s/^\s+//; my @cols = split /\s+/, $line; my $key = join '-',splice @cols, 0, 2; my %com; @com{qw( In2017 In2018 In2019} = @cols; $hash{$key} = { %com }; } use Data::Dumper; $Data::Dumper::Sortkeys=1; print Dumper(\%hash);
        Can some one help me out

        The code you posted does not even compile:

        $ perl -cw 1230041.pl Can't find string terminator ")" anywhere before EOF at 1230041.pl lin +e 17.

        Here's what I would suggest in your situation.

        1. Fix all the compilation errors.
        2. Go through the resulting code and try to explain to yourself what each individual line is doing and how it is performing that action. If you can't do that for any of the lines, remove them and replace them with something simpler which you do understand.
        3. Having done 1 and 2 you now have some code which runs and which you understand. Now create an algorithm for anything else that you want the script to do.
        4. Code up the new algorithm.
        5. Test the new code. If it fails to meet your requirements go to 1.
        I doubt the code works. Where does the qw operator end?

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: hash of hash
by AnomalousMonk (Archbishop) on Feb 15, 2019 at 17:43 UTC
    output:
         {'Hyundai' => {'In2017' => 20, 'In2018' => 30, 'In2019' => 50}}

    In addition to the other problems and objections mentioned, I can't even figure out how the quoted output relates to the example input. If you're going to cheat, at least cheat coherently.


    Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-24 06:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found