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.
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.
| [reply] |
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]
| [reply] [d/l] |
|
#!/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);
| [reply] [d/l] |
|
$ 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.
- Fix all the compilation errors.
- 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.
- 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.
- Code up the new algorithm.
- Test the new code. If it fails to meet your requirements go to 1.
| [reply] [d/l] |
|
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]
| [reply] [d/l] [select] |
|
|
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: <%-{-{-{-<
| [reply] [d/l] [select] |
|
|