Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Perl-Sensitive Sunglasses
 
PerlMonks  

<> diamond Operator

by kocaweb (Initiate)
on Apr 06, 2005 at 18:42 UTC ( #445396=perlquestion: print w/ replies, xml ) Need Help??
kocaweb has asked for the wisdom of the Perl Monks concerning the following question:

Hi Guys, I have this assignment to do for school in perl. I'll i wanna do is pass this course but i have no clue on how to to programming in perl. could any one please help me I am submiting the assignment my teacher told me that it would take 10 minutes. but there is no way i can figure out this. thanks foxs. there are 5 files that should be processed with this script. they just contain different words.it should use <> operator read the records on each file and if it matches change the records. user should enter files in command line argument.


Create a script named "lab08-1.pl" 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 has specific text information
If any record has the text "xxx" in any case
(upper, lower, or mixed), change the text to "ggg" in all lower case
If any record has the text "alpha" in lower case only, change the text to upper case
o If the file name is file1 copy this file to the sysbacks folder.
o If the file name is file2 copy this file to the alpha folder
o If the file name is file3 copy this file to the blue folder under gamma
o If the file name is file4 or file5 copy these files to the green folder under gamma.

Look Guys, my Major is not computers i had to take a computer course to get the degree, the only reason i took perl is because it sounded interesting i didnt even know that is a programming language, and to be honest with you, i dont even know how to start, thats why i asked if somebody could do it for me or at least start then i could take it from there, if you dont want to do it and money is in your mind, i could pay for your service, just keep in mind I am a student.The book that i have has a lama on the cover,if it was something that i would do everyday i would learn perl, but once i pass this course may be i wont even work on computer anymore, and there are only 2 assignments on this course, test i have no problem i could just read the book and somehow pass it.

Comment on <> diamond Operator
Re: <> diamond Operator
by sh1tn (Priest) on Apr 06, 2005 at 18:49 UTC
    And you expect applause for posting your homework right here , I suppose?


      And you expect applause for posting your homework right here , I suppose?

      It's not like he was trying to hide the fact that it was homework. (Not like he could have, with an assignment like this, but some people try.) He probably really feels overwhelmed. I do wish he'd given us more info about the problem he's having, the textbook the class is using, and so on; we maybe could have helped him better then. Just saying "we won't do your homework for you" doesn't really help very much. (Okay, so it's better than just giving him the answer to the assignment. But it's not much better than saying nothing at all.)

Re: <> diamond Operator
by Joost (Canon) on Apr 06, 2005 at 18:51 UTC
Re: <> diamond Operator
by ikegami (Pope) on Apr 06, 2005 at 18:52 UTC

    Which part do you need help with? What have you tried? We're not going to do your homework for you.

    <FILEHANDLE> and the special case <> are documented in the section "I/O Operators" of perlop

Re: <> diamond Operator
by kutsu (Priest) on Apr 06, 2005 at 19:45 UTC

    If you want to pass a perl course....learn perl. Our Tutorials Section should help (Getting Started with Perl, esp.) - woofly's tutorial (Where and how to start learning Perl) is an excellent example of this

    Then when you get specific questions ask here.

    "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce

Re: <> diamond Operator
by Anonymous Monk on Apr 06, 2005 at 19:50 UTC

    I wouldn't use:

    $lc_str = $str | ("\x20" x (length $str)); push @index, $-[0] while $lc_str =~ /xxx/g; substr($str, $_, 3) = 'ggg' foreach @index;

    I also wouldn't use the diamond operator.

      yeah, also don't turn in something like this:

      undef$/;for my$f{open IN,$f xor<IN>;s/(x)\1{2}/'g'x3/iegm;s/alpha/uc$1 +/egm;open OUT =>'>',($f eq'file1')?'sysbacks/file1':(($f eq'file2')?'alpha/file2':(( +$f eq 'file3' )?'gamma/blue':(($f=~/^file[45]$/)?'gamma/green':'/dev/null')))or die; +print OUT $_; }@ARGV;

      Even if it works (which I didn't test), your prof will not give you a good grade for it.

Re: <> diamond Operator
by jonadab (Parson) on Apr 06, 2005 at 21:22 UTC
    my teacher told me that it would take 10 minutes

    Right, but that's after you read the chapter. Obviously, if you haven't read the chapter, you won't know how to complete the assignment. What textbook are you using? (The prof *did* assign a textbook, I hope...) Does it have a picture of a camel on the front? Or a llama? (If it says "21 days" on the cover, you should probably hightail it to a library and get one of the ones with an animal on the cover to go along with it. They're much better.) We could probably point you to the right section of the textbook; OTOH, your professor probably can too, and may have already. You'll want to read the chapter before attempting the assignment.

    Of course, we could also probably just give you the answer to this assignment. (Ten minutes? I could do it in ninety seconds.) But that won't help you pass the class. It'll get you through this assignment, but if you don't understand this one, you'll be in over your head soon, as the class moves on to more involved programming. If you're going to pass the course, you need to understand the answer you turn in.

    update:
    Look Guys, my Major is not computers i had to take a computer course to get the degree, the only reason i took perl is because it sounded interesting

    Perl is interesting, and a lot of fun. We all (well, all of us with degrees) had to take courses outside our majors. My major was Math, but I took a drawing class, an Appreciation of Fine Arts class, and other things. And you don't have to be a computer major to enjoy dabbling in using computers just a little. We're not asking you to achieve Guru status, and neither does your professor expect that. But you will have to learn something.

    to be honest with you, i dont even know how to start, thats why i asked if somebody could do it for me or at least start then i could take it from there

    We're trying to help you know where to start. If we just start the problem for you, you will not very likely be able to <q>take it from there</q>. Computer programming, like math and foreign languages, is inherently cumulative. It's not like History, where once you pass the quiz on the first chapter you can move on to other things. The very first things you have to learn, you still need to understand to do the work at the very end of the course, and to pass the exam.

    if you dont want to do it and money is in your mind, i could pay for your service, just keep in mind I am a student.

    You'd have to pay one of us to take the exam for you too, including travel expenses to your university and whatever it takes to buy off your professor to look the other way when we don't look like you. As a student, you can't afford that. And then there's the risk of getting caught, not to mention the value of your personal integrity.

    The book that i have has a lama on the cover,

    Excellent. That's widely regarded as a good one, and is probably the best choice for you if you've never programmed before. I learned Perl from the Camel book, which is better if you have previous programming experience in other languages, but the Camel book starts out a little steeply for someone with no prior experience, so the Llama is probably the best possible textbook you could have for the course, in your situation.

    if it was something that i would do everyday i would learn perl, but once i pass this course may be i wont even work on computer anymore,

    Maybe, if you take the time to learn Perl, you'll change your mind about that, and decide to do a little programming on the side -- nothing complicated, just for a hobby. Or maybe not, but wouldn't you like to explore the possibility?

    and there are only 2 assignments on this course, test i have no problem i could just read the book and somehow pass it.

    I don't think you'll be able to pass the test, or the course, without learning enough Perl to complete this assignment. The test will have questions on it that require you to write some short bits of code -- nothing complicated, but then, this assignment isn't very complicated either. It is something you'll be able to do.

    So, get out that textbook and start looking it over. To complete this assignment, you'll need to learn the basics of just about half a dozen Perl features: print (start with that one, and you can complete the first requirement listed in the assigment just with that), @ARGV (and probably arrays in general), open, reading from filehandles (with the <DIAMOND> operator), pattern matching and substitution (notably, s///), and conditionals (i.e., if). None of these, individually, is very hard, since you only need to learn their basic features, not every nuance. If you have a specific question about any of these, or if there is some particular point you don't understand, ask us. We do try to be helpful, really. But just giving you an answer to the assignment, as much as you might think it would help, would not, in fact, help you -- not in the long run, and indeed almost certainly not even in your stated goal of passing the course.


    "In adjectives, with the addition of inflectional endings, a changeable long vowel (Qamets or Tsere) in an open, propretonic syllable will reduce to Vocal Shewa. This type of change occurs when the open, pretonic syllable of the masculine singular adjective becomes propretonic with the addition of inflectional endings."  — Pratico & Van Pelt, BBHG, p68
Re: <> diamond Operator
by fraktalisman (Hermit) on Apr 06, 2005 at 22:14 UTC
    kocaweb wrote:
    I'll i wanna do is pass this course

    That's very sad. Then didn't you choose to be in the programming class? In which kind of school is Perl mandatory? I am impressed (of the school). I have met so many people who are doing some kind of course or class to make them programmers or web designers or both, and many of them even get their certificates even though the showed little talent or effort. But I am not sure if you'll become one of them. Because:

    At least, kocaweb, you achieved some things: you wrote what has to be done. You have looked somewhere to find information. You even got information in the answers, hopefully you figured out the right chapter in the perl documentation and understood it. After all, you might be spending much more than ten minutes to get your homework done, but then maybe you will have learned more in the end, and this experience will help you for your next homework. Maybe it even gets you interested in the stuff you are about to learn - not only passing the exam. Now you've found Perlmonks, take some spare time and explore the site, it's a bit unconventional at first glance, but it's worth being discovered.

      look guys, my major is not in computers, I had to take one of the computer courses to finish my degree, i took perl just because the name sounded interesting, to be honest with you i dont know even how to start, thats why i asked if somebody can do it for me or at least start and then i could take it from there, and if you dont wanna help and money is in your mind , i am willing to pay for your service. Just have in mind that I am a student.

        It's not that we have a problem helping. That's what the whole site is about - if we didn't like helping, we would be elsewhere.

        It's that we generally don't like doing it for someone.

        It's just a lot easier to help someone who shows some effort than someone who shows no effort.

        My wife had to take a computer course for her degree, too. She took a course that taught QBasic on the Mac. She got mad at me for not doing some of it for her, too. But, when she completed the course, having written some really basic (no pun intended) programs, she was quite proud of them. No way she could write a lick of code now, but she was proud of it at the time. Got a good mark, too, if I recall correctly. But all I did was help - not do it for her.

        So, if you could show what you've attempted so far, and where it isn't doing what you think it should, there will be people falling over each other to help you. Really.

Re: <> diamond Operator
by Mr. Muskrat (Abbot) on Apr 07, 2005 at 00:35 UTC

    I honestly don't know how you made it to your 8th lab without knowing anything about Perl...

    I realize that you're not majoring in computers. You did however sign up for the course! If you fail it, you'll have to start fresh with the same class or a class that very well could be much harder. If you read the textbook and do the assignments yourself, you should pass.

    You say the book has a llama on the cover so I'm guessing that it's "Learning Perl". You don't mention what edition though. Read every chapter up to and including the chapter on "Basic I/O". This will get you familiar with the things that you will need to finish this homework assignment. The chapter on "Basic I/O" will explain <> (the diamond operator).

Re: <> diamond Operator
by starbolin (Hermit) on Apr 07, 2005 at 00:58 UTC
    but once i pass this course may be i wont even work on computer anymore

    Don't sell yourself short. Many jobs involve computers nowdays. People with computers skills earn more money in any occupation. Learn how to use computers.

    starbolin
Re: Solution for your prof
by nikos (Scribe) on Apr 07, 2005 at 17:28 UTC
    Hi!

    I guess all of us have told him too much about learning Perl and studying at school. Enougn is said and done. He should realize he has to learn basics of Perl if he wants to pass the course. It's up to him to decide what to do with his own life. We did all we could to help him. I think the time has come to let him do whatever he wants to do with the solution of the assignment.

    #!/usr/bin/perl use strict; use warnings; print "First_name Last_name\n"; foreach my $file (@ARGV) { open(FILE, "<$file"); if( $file eq "file1" ) { $file="sysbacks/file1"; } elsif ( $file eq "file2" ) { $file="alpha/file2" } elsif ( $file eq "file3" ) { $file="gamma/blue/file3"; } elsif ( ($file eq "file4") || ( $file eq "file5" ) ) { $file="gamma/green/$file"; } open(OUTPUT, ">$file"); while(<FILE>) { s/xxx/ggg/gi; s/alpha/ALPHA/g; print OUTPUT; } close(FILE); close(OUTPUT); }

    P.S. Tested on perl 5.6.1 build for i386-freebsd
    P.S.S. It's been written this way to make his professor believe he wrote it.
    P.S.S.S. I think his prof won't understand jonadab's solution. I don't expect him to be a perlmonk. I might be wrong. I'm sorry I didn't want to offend him.

      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.

        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}
      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

        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}
      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://445396]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (12)
As of 2014-04-16 20:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (433 votes), past polls