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

I am seeking the wisdom of the perl monks...

For some reason I don't know why my code does not work. I have commented on the lines that are giving me a problem.

If anyone could give me any insight I would love it.

Thanks agian for your time.

Satanya

"Dadis_Ramsey_lab4.pl" 193 lines, 5620 characters #!/usr/bin/perl print "This program is designed to sort files containing a list of:\n" +; print "First Names, Last Names, Ages, Sexes, Heights, and Weights\n"; $inputfile = "lab4.txt.33"; openfile(); sub Menu { print "\nHere is a list to select from:\n"; print "A. Set input filename\n"; print "B. Load file\n"; print "C. Sort by Last Name\n"; print "D. Sort by First Name\n"; print "E. Sort by Age\n"; print "F. Sort by Height\n"; print "G. Sort by Weight\n"; print "Q. Quit\n"; print "Enter your Selection: "; chomp ( $selec = <STDIN> ); $selec =~ s/[a-z]/[A-Z]/; } if ( $selec eq 'A' ) { namefile(); Menu (); } elsif ( $selec eq 'B' ) { openfile(); } elsif ( $selec eq 'C' ) { sortlast(); Menu(); } elsif ( $selec eq 'D' ) { sortfirst(); Menu(); } elsif ( $selec eq 'E' ) { sortage(); Menu(); } elsif ( $selec eq 'F' ) { sortsex(); Menu(); } elsif ( $selec eq 'G' ) { sortheight(); Menu(); } elsif ( $selec eq 'H' ) { sortweight(); Menu(); } elsif ( $selec eq 'I' ) { print "\n\nGood Bye..."; exit; } else { print "You have entered an invalid selection.\n"; print "Please try again:\n"; Menu (); } while ( <File> ) { chomp; ( $last, $first, $age, $sex, $height, $weight, $comment ) = split (/,/); push( @last, $last); push( @first, $first); push( @age, $age); push( @sex, $sex); push( @height, $height); push( @weight, $weight); push( @comment, $comment); } sub sortage() { $num = @age; for ( $i = 0; $i <= $num; i++) { # something is wrong her +e for($j = $i+1; $j = $num; $j++) { # and here if ( $age[$i] > $age[$j] ) { @age[$i,$j] = @age[$j,$i]; @last[$i,$j] = @last[$j,$i]; @first[$i,$j] = @first[$j,$i]; @sex[$i,$j] = @sex[$j,$i]; @height[$i,$j] = @height[$j,$i]; @weight[$i,$j] = @weight[$j,$i]; @comment[$i,$j] = @comment[$j,$i]; } # And here } } } sub openfile( ) { open ( File, $inputfile) || die "failure: $!"; @file = < File >; close ( File ); Menu (); } sortlast() { $num = @last; for ( $i = 0; $i <= $num; $i++) { for($j = $i+1; $j = $num; $j++) { if ( $last[$i] > $last[$j] ) { @age[$i,$j] = @age[$j,$i]; @last[$i,$j] = @last[$j,$i]; @first[$i,$j] = @first[$j,$i]; @sex[$i,$j] = @sex[$j,$i]; @height[$i,$j] = @height[$j,$i]; @weight[$i,$j] = @weight[$j,$i]; @comment[$i,$j] = @comment[$j,$i]; } } } }

Replies are listed 'Best First'.
Re: Please help...not certain why my code does not work.
by srawls (Friar) on Jun 05, 2001 at 00:42 UTC
    When you have constant prints like this
    print "\nHere is a list to select from:\n"; print "A. Set input filename\n"; ...
    It could be better writen with here documents:
    print END; This is text that only needs one print END
    There were so many places where you made an error, I thought I'd like to point this out: good job on using <STDIN> instead of <>.
    chomp ( $selec = <STDIN> );
    Where you use this:
    $selec =~ s/[a-z]/[A-Z]/;
    The substitution could be better written:
    $selec =~ tr/a-z/A-Z/;
    and that could be better written without a regex:
    $selec = uc($selec);
    Where you have the switch like statement:
    if ( $selec eq 'A' ) { namefile(); Menu (); } ...
    You might want to look into something like this:
    %func = (A => \&namefile, B=> \&openfile, ...) ... $func{$selec}->(); Menu();
    Here, you could do better to add a 'my'
    my ( $last, $first, $age, $sex, $height, $weight, $comment ) = split ( +/,/);
    Also, you might want to look into having a data structure somewhat like this:
    $age[$i++] ={last=>$last,first=>$first,age=>$age,sex=>$sex,height=>$he +ight,weight=>$weight,comment=>$comment}
    And you would use it like this:
    print "First last=$age[0]{last}\n";
    Also, if you change the datastructure, you might want to look into a Shwartzian Transfer.

    Sorry for the briefness on some topics, but there were a lot : ) If you still have questions, just ask.

    The 15 year old, freshman programmer,
    Stephen Rawls

      Thanks for your help Stephen.

      I am a student right now.. I have not been 15 in 5 years but I am greatfull for any help. (wow I feel old... now)

      I am trying to complete my programing degree.

      So any help that I get is fully appreciated.. and I learn alot from you all.

      Thanks agian.

      Satanya

Re: Please help...not certain why my code does not work.
by arturo (Vicar) on Jun 05, 2001 at 00:12 UTC

    One place you're going wrong (and, sorry to be blunt, but there are a *lot* of places where you're going wrong here):

    Your openfile subroutine loads all the lines of the file into the @file array, and closes the file. Yet later, when you're trying to sort the data, you're attempting to read from the filehandle you've already closed. Instead, loop over the array that holds the file's contents (you might check out map for this sort of thing).

    Another place: check out Perl's built-in sort function, it will save you many a headache. (perlfunc:sort or perldoc -f sort on your own system).

    Good luck!

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'

      no problem... this is the only way I am going to learn.

      I am still really new at this... so all of your help is greatly appreciated.

      Thanks again

      Satanya

Re: Please help...not certain why my code does not work.
by suaveant (Parson) on Jun 05, 2001 at 00:05 UTC
    for ( $i = 0; $i <= $num; i++) {              # something is wrong here
    i++ should be $i++

    for($j = $i+1; $j = $num; $j++) {
    $j = $num; should be $j == $num;
    although I doubt that's what you want, you probably want $j <= $num;

    dunno about the }... Update tried the code for subroutine with fixed code, no error on that }

                    - Ant

      Thanks for your help. Greatfull trainee