in reply to <> diamond Operator

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re^2: Solution for your prof
by kocaweb (Initiate) on Apr 07, 2005 at 19:41 UTC

    Thanks a lot guys, and you were right I should start learning some perl, I didn't realize How far behind I was. I did start some reading I did lot of research on google read lot of scripts, and I started to like perl. Maybe I am at the wrong forum( if I am could point me to the right direction)., cuz I am just a beginner. And I think perlmonks is for more advanced help. You may laugh at my problems, but everybody is at different stage of knowledge. any ways. I did start lab09 on my own today. Could you please tell me guys what do you think and if there is any errors, here is lab 9 and I am really sorry if I am waisting your time. this would be my last thing asking.


    Script 1
    Create a script named “lab09-1” script to:
    Display your full name (first and last) on the 1st line
    The script should
    o accept filenames as command line arguments
    o each record in the file is a numeric value
    o accumulate each value into a total amount field
    o print each value (zero decimal places) formatted as follows:


    File name Value (print heading)
    Filename1 1000 (print each detail value indicating the file it is from)

    o when all records from all files have been processed print the total
    Display a message on the last line indicating the script is finishing
    Filenames: lab6, myfile, alpha, bones

    here is the script

    #!/perl/bin/perl
    #
    # Author: L.H
    # Script Name: lab6-1
    # Date Written: March 15, 2005
    #Purpose: Basic I/0
    #
    #

    foreach $file (@ARGV) {
    open(FILE, "$file");


    while (<FILE>) {

    $sum+=$_;
    print "$file $_";
    }
    print "$sum";
    }

    but I still can't get total for each file, so far I managed to get the total for one file only.

      Use <code> tags when posting code snippets. :-)

      You're actually getting the totals for all the files. Try the following:

      #!/perl/bin/perl # # Author: L.H # Script Name: lab6-1 # Date Written: March 15, 2005 # Purpose: Basic I/0 # # use strict; foreach my $file (@ARGV) { open(FILE, "$file"); my $sum = 0; while (<FILE>) { $sum+=$_; print "$file $_\n"; } print "$sum\n"; close( FILE ); }

      The major change is the my $sum = 0; line. It scopes $sum to be within the foreach-loop. This means that it gets reset every time. You'll notice I added strict to my version. This provides some typo-checking, and also requires that you declare your variables (preferably with my) beforehand.

      This node was much better than your previous nodes. You posted what you tried and why it didn't work. This means we'll help you out, explaining why what you did didn't work and how to improve it. We'll edit, not create.

        wow guys, I really feel good know, actually I am learning lot of stuff, I've been awake from 7 am. and learning pearl all day long. Here is the script tha I modified. The script should do the following: copy all the files that were entered in command line to a folder lab06d which is already created. and add a "b" at the end of the file to tell that is a backup. also it should print the total value added from all the files. but I don't thing is adding the value from all the files together. here is the script again:

        #!/perl/bin/perl
        #
        # Author: L.H
        # Script Name: lab6-1
        # Date Written: March 15, 2005
        # Purpose: Basic I/0
        #
        #
        print "L.H\n";
        print "Starting Script\n\n";
        print "Filename\tValue\t(print heading)\n";

        use File::Copy;
        use strict;
        my $total;
        foreach my $file (@ARGV)
        {
        open(FILE, "$file");

        my $sum = 0;
        while (<FILE>)
        {
        chomp();
        $sum+=$_;
        print "$file $_\n";
        $total+=$sum;
        }
        print "Total value for the $file : $sum\n";
        close( FILE );
        }
        print "Total value accumulated from all the files
        is:$total";

        foreach my $file (@ARGV)
        {
        my $newfile=$file . "b";
        copy ($file, "lab06d\\$newfile");
        }

      I gave you a plus vote for taking your medicine and coming back again. I'd give you another plus for a well written second question if I could. I noticed someone still downvoted you on your second question showing perhaps still some bias towards homework questions. Or it could because of the reference to Perl Monks being too advanced.

      There are all levels of programmers here at the Monastery and many basic questions asked and many basic questions answered. We all think this is the best forum for Perl you'll find and would encourage you to keep returning. There also is also a lot of good basic material available here, some of it was already pointed out to you and you have the book. We'll expect you to do your research before posting as you've already seen. Please feel free to come here for help and bring a thick skin.

      P.S. You'll get a wider readership on new questions if you start a new node rather than posting as a response to an existing node.


      s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}
Re^2: Solution for your prof
by nikos (Scribe) on Apr 07, 2005 at 20:23 UTC
    Why do you, monks, give negative votes for my post? Please explain me what I did wrong and I hope I won't do it anymore. Thank you in advance!
      I can't speak for all down voting, but...
      Doing someone else's homework, in it's entirety, is bad in many ways:
      • The person asking will learn nothing, or very little
      • it can be construed to be cheating
      • it will encourage others to ask
      • and lots of people asking us to do homework assignments will end up harming this community
      -Scott
        First of all, thanks for a reply. I agree with 'it will encourage others to ask' and 'lots of people asking us to do homework assignments will end up harming this community'. We explained him why he should learn it. Many things were said. I just thought it was the time to tell him how to do it and let him do whatever he wants to do with the solution. BTW it's not that bad to point out a solution when someone learns new things. I agreee I gave him the done assigment but still... it could help him to understand what he was asked for and do the next task by himself. It's really about how much to help. I think we can discuss it forever. I agree we shouldn't tell others how to do their homework, we should help to correct mistakes in one's code but not to create it as someone said in the thread. Monks warned him and explained him why he had to learn perl. That's why I posted that script for him. BTW I saw questions like 'is there any module to do ...' it's the same sort of questions. I think we should answer such questions. I agree this situation was different as he stated he didn't want to learn. Anyways it's really should be posted to 'PerlMonks Discussion'. I'm a little afraid to post it there. I just think it will get negative feedback as it did and that's about it, nothing else. I just do not understand the rules of the monastery sometimes. That's just me... I'm sorry if I broke them. I'll do my best to be a 'good monk' hehe. Thank you again!

      He's falling under the spell of Perl Monks already.

      I wrote some stuff I was going to post here but this node already covers it Why do I get such negative votes?. In addition; Complaining about down votes is the fastest way to get down votes. I didn't down vote this because you were asking a question but you may want to avoid this area in the future. You are doing very well, keep at it, keep learning, we'll be here.

      You could always try a compliment on my clever signature;-)


      s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}
Re^2: Solution for your prof
by inman (Curate) on Apr 08, 2005 at 07:59 UTC
    You may get better marks if you populated a hash with the filename / directory pairs since this could be populated from a configuration file.
    my %destinations = qw( file1 sysbacks file2 alpha file3 gamma/blue file4 gamma/green file5 gamma/green ); #later my $target = "$destinations{$filename}/$filename";

    I take your point about making his professor believe it was him. Even this code is too well written for a newbie. Try the following newbie style tips...

    1. Remove the elsif and hust have a bunch of if statements one after the other.
    2. Use a regex for the if test but manually assign $_ = $file beforehand.
    3. chomp the input on the way in.
    4. Use the $_ explicitly in the print statement with a newline to make up for the chomp!
    5. Use a different variable to store the name of the output file.