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

Re: here's my code...

Expand Messages
  • Allan Dystrup
    ... sort ... A fast answer and some pointers at where your program goes astray: in the while( @employee = } { ... @x=sort(@employee); foreach
    Message 1 of 6 , Jul 3, 2004
    • 0 Attachment
      --- 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";
      }

      ______________________________________________________________
    • merlyn@stonehenge.com
      ... Kathryn I am not allowed to use the cmp operator to sort. I don t understand this phrase, so I didn t read the rest of your message. How are you not
      Message 2 of 6 , Jul 3, 2004
      • 0 Attachment
        >>>>> "Kathryn" == Kathryn Tate <kathryntate@...> writes:

        Kathryn> I am not allowed to use the cmp operator to sort.

        I don't understand this phrase, so I didn't read the rest of your
        message. How are you "not allowed"? Does your boss come to you and
        dictate exactly what operators can go in your program? Tell me
        where you work so I can avoid working there.

        --
        Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
        <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
        Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
        See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
      • Charles K. Clarkson
        ... Is this homework? It is important that you do your own homework. Most teachers won t assign something you can t figure out from previous lessons.
        Message 3 of 6 , Jul 3, 2004
        • 0 Attachment
          Kathryn Tate <kathryntate@...> wrote:

          : I am not allowed to use the cmp operator to sort.

          Is this homework?

          It is important that you do your own homework.
          Most teachers won't assign something you can't
          figure out from previous lessons. Programming is
          about problem solving. Learning to solve problems
          without our help will be much better for you in the
          long term.


          HTH,

          Charles K. Clarkson
          --
          Mobile Homes Specialist
          254 968-8328
        • Kathryn Tate
          ... Absolutely. But we are allowed to get assistance where needed--as long as someone isn t writing the code for you. I am simply asking for tips on printf()
          Message 4 of 6 , Jul 3, 2004
          • 0 Attachment
            On Jul 3, 2004, at 7:12 AM, Charles K. Clarkson wrote:

            >
            >     It is important that you do your own homework.
            > Most teachers won't assign something you can't
            > figure out from previous lessons. Programming is
            > about problem solving. Learning to solve problems
            > without our help will be much better for you in the
            > long term.

            Absolutely. But we are allowed to get assistance where needed--as long
            as someone isn't writing the code for you. I am simply asking
            for tips on printf() and I wish to use the cmp operator to sort but
            can't and
            needed some input on this.
            I hope you feel this is going by the rules. I am comfortable with this.
            (And I deem myself quite honorable).

            Thanks,
            Kathryn





            [Non-text portions of this message have been removed]
          • 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 5 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.