in reply to Newbie question under time constraint
package Employee; use strict; use warnings; sub new{ my ($class, $args) = @_; my $self = bless { name=> $args->{name}, hourlywage => $args->{hourlywage}, hours => $args->{hours} }, $class; } sub pay { my $self = shift; return $self->{hours} * $self->{hourlywage}; } sub gist { my $self = shift; return "Employee info: name: $self->{name} hours: $self->{hours} w +age: $self->{hourlywage}\n" } 1; package EmployeeList; use diagnostics; use strict; use warnings; sub new{ my ($class) = @_; my $self = bless { EmployeeList => [] }, $class; return $self; } sub add_Employee { my $self = shift; my $Employee = shift; push @{ $self->{EmployeeList} }, $Employee; } sub gist{ my $self = shift; my @gist = map { $_->gist } @{ $self->{EmployeeList} }; return join "", @gist; } sub average_hourly_wage{#not even sure where to begin, don't know how +to initialize or access the hash at this point my $self = shift; my $wages; my $count; foreach my $employee ( @{ $self->{EmployeeList} }) { $wages += $employee->{hourlywage}; $count++; } return $wages / $count; } sub find { my $self = shift; my $name = shift; foreach my $Employee ( @{ $self->{EmployeeList} } ) { return $Employee if $Employee->{name} eq $name; } } 1; #use Employee; #use EmployeeList; print "Enter the name of the file to open: "; my $input_file = <STDIN>; chomp $input_file; open(my $file_handle, "<", $input_file) or die "failed to open < input +_file: $!\n"; my $EmployeeList = new EmployeeList(); while (my $row = <$file_handle>){ my @splitrow = split ' ', $row; my $Employee1 = Employee->new({ name =>$splitrow[0], hourlywage =>$splitrow[1], hours =>$splitrow[2] }); $EmployeeList->add_Employee( $Employee1 ); } print $EmployeeList->gist(); print $EmployeeList->average_hourly_wage(), "\n"; print $EmployeeList->find("holli")->pay, "\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Newbie question under time constraint
by choroba (Cardinal) on Aug 04, 2019 at 22:01 UTC | |
|
Re^2: Newbie question under time constraint
by Your Mother (Archbishop) on Aug 04, 2019 at 22:38 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |