Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Re: here's my code...

Expand Messages
  • Kathryn Tate
    Thank you so much. We have gone over hashes so that is allowed in our code. I will try this as I cannot use the cmp operator--as far as I know. Very nice.
    Message 1 of 6 , Jul 3, 2004
    • 0 Attachment
      Thank you so much. We have gone over hashes so that is allowed in
      our code. I will try this as I cannot use the cmp operator--as far as
      I know.

      Very nice.

      Thanks,
      Kathryn


      On Jul 3, 2004, at 3:29 AM, Allan Dystrup wrote:

      > --- In perl-beginner@yahoogroups.com, Kathryn Tate <kathryntate@c...>
      > wrote:
      > > I am not allowed to use the cmp operator to sort. And this will not
      > sort
      > > plus mucho problemo with printf():
      > >
      >
      >
      > A fast answer and some pointers at where your program goes astray:
      >
      > in the
      > � while( @employee = <SOURCE> } {
      > ���� ...
      > ���� @x=sort(@employee);
      > ���� foreach (@employee)�� {
      > ������� print_employee($_);
      > ���� }
      > � }
      > construct you are reading ONE line (record) from the employee data
      > file, and sorting this list in standard (string comp) order. Then
      > you're sending each FIELD of the list to the print_employee function.
      > This is probably not what you want.
      >
      > Your algorith should be to read all records of the data file into an
      > internal data structure, sort the data strunt, and then print out a
      > formated report based on the sorted struct.
      >
      > There are several ways to do this, one would be to 'slurp' the data
      > into a hash with the person names as keys and the person detail array
      > as a value, sort the hash on the keys, and call the printing function
      > with the sorted records as param.
      >
      > This is not necessarily the best and probably not the simplest way to
      > do it, but maybe you can chew on it untill some of the wizzards in
      > here picks up the wand and gives it a proper waving.
      >
      > best regards /
      > allan dystrup
      >
      > and a little hacking
      > ___________________________________________________________________
      > #!/usr/bin/perl -w
      > use strict;
      >
      > sub print_employee {
      > �� my ($person, $number, $dept, $salary);
      > �� my ($first, $last, $sum, $final);
      > �� ($person, $number, $dept, $salary)=split(" ",$_[0]);
      > �� $person=~s/,/ /g;
      > �� $person=~tr/A-Z/a-z/;
      > �� ($first, $last)=split(" ", $person);
      >
      > �� if ($dept=~/ACCT/)
      > ����� { $sum= $salary * 1.06;}
      > �� elsif ($dept=~/ENG/)
      > ����� {$sum= $salary * 1.08;}
      > �� else
      > ����� {$sum= $salary * 1.04;}
      >
      > �� $final=sprintf(" %s, %s%-10s%-10s%-10d", ucfirst($last),
      > �� ucfirst($first), $number, $dept, $sum);
      >
      > �� print DESTINATION $final, "\n";
      > �� print $final, "\n";
      > }
      >
      > �� my ($file, @employee, @heading, @x);
      > �� open(SOURCE, "<employee_file") || die "Can't open employee_file: $!
      > \n";
      > �� $file="newfile";
      > �� open(DESTINATION, ">$file") || die "Can't open newfile: $!\n";
      >
      > �� #while( @employee = <SOURCE> }
      > �� #{
      > �� #�� @heading=splice(@employee, 0, 6);
      > �� #�� print "@heading \n";
      > �� #�� print DESTINATION "@heading \n";
      > �� #�� @x=sort(@employee);
      > �� #�� foreach (@employee)�� {
      > �� #����� print_employee($_);
      > �� #�� }
      > �� #}
      > ��
      > �� my (%employees, $emp);
      > ��
      > �� # Read employees into hash data struct
      > �� while ( $emp=<SOURCE> ) {
      > ����� chomp($emp);
      > ����� my @emp = split(" ", $emp);
      > ����� $emp = shift(@emp);� # emp name is key, assume unique
      > ����� $employees{$emp} = \@emp;� # emp details is value (ref. to
      > array)
      > �� }
      >
      > �� # Sort employee data on hash key, and call print function
      > �� foreach $emp (sort keys %employees) {
      > ����� &print_emp( $emp, $employees{$emp} ); # pass name and ref to
      > details array
      > �� }
      > �
      > �� # Do whatever formating necessary for prettyprint
      > �� sub print_emp() {
      > ����� print "$_[0], @{$_[1]}\n";
      > �� }
      >
      > ______________________________________________________________
      >
      >
      >
      > Unsubscribing info is here:
      > http://help.yahoo.com/help/us/groups/groups-32.html
      >
      >
      >
      >
      > Yahoo! Groups Sponsor
      >
      > ADVERTISEMENT
      > <lrec_companion_043004.gif>
      > <l.gif>
      >
      > Yahoo! Groups Links
      >
      > � To visit your group on the web, go to:
      > http://groups.yahoo.com/group/perl-beginner/
      > �
      > � To unsubscribe from this group, send an email to:
      > perl-beginner-unsubscribe@yahoogroups.com
      > �
      > � Your use of Yahoo! Groups is subject to the Yahoo! Terms of
      > Service.
      >
      >

      [Non-text portions of this message have been removed]
    Your message has been successfully submitted and would be delivered to recipients shortly.