CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
need some help to write a simple script

 



leonchik1976
New User

Dec 18, 2013, 9:40 AM

Post #1 of 8 (1125 views)
need some help to write a simple script Can't Post

I need help to write a script named P1 that takes filename as input, creates P1.half as output, that contains content of original file, but every word with even length must be replaced with it first half.
for example:
if original file contains:
aab ccccf 123A
ccc 123a wwwwww ccc 61234 77
88888 uuuu 2 uuuuu ccccf

P1.half must contain:
aab ccccf 12
ccc 12 www ccc 61234 7
88888 uu 2 uuuuu ccccf


Kenosis
User

Dec 18, 2013, 10:22 AM

Post #2 of 8 (1121 views)
Re: [leonchik1976] need some help to write a simple script [In reply to] Can't Post

Is this a course assignment?


leonchik1976
New User

Dec 18, 2013, 10:23 AM

Post #3 of 8 (1118 views)
Re: [Kenosis] need some help to write a simple script [In reply to] Can't Post

no


Kenosis
User

Dec 18, 2013, 10:27 AM

Post #4 of 8 (1114 views)
Re: [leonchik1976] need some help to write a simple script [In reply to] Can't Post

Really? The link is to a document with the exact same problem given last spring, 21/03/2013.


zapzap
User

Jan 5, 2014, 2:27 AM

Post #5 of 8 (922 views)
Re: [leonchik1976] need some help to write a simple script [In reply to] Can't Post

Greetings.

I do not really care if this is a homework assignment or not. If you need help in a perl homework assignment then this is the place.

In fact I find it highly illogical to shun individuals with homework but yet rush to the aid of those that are working professionals who earn a salary for the assistance they receive here.


Code
The assumption is that you pass your input file as an argument: 
$ perl script.pl inputfile
After the script is run you should see a new file with your desired output


Code
#!/usr/bin/perl 
use strict;
use warnings;

open my $fh, '>','P1.half' or die "Cannot open P1.half for writing: $!\n";
while( <> ) {
my $str = '';
for ( split ) {
my $len = length;
$str .= $len % 2 == 1 ? "$_ ": substr($_,0,$len/2) . " ";
}
print $fh "$str\n";
}


peace
zap


BillKSmith
Veteran

Jan 5, 2014, 9:00 AM

Post #6 of 8 (912 views)
Re: [zapzap] need some help to write a simple script [In reply to] Can't Post

In general, a student and a professional need a different kind of help. A student, by definition, is trying to learn Perl. He needs a hint to help him solve the problem himself. A complete solution seldom helps him toward this goal. The professional usually needs a solution to his immediate problem. Clearly it is desirable if he learns from the experience, but he needs the solution.

We ask when it is not clear which kind of help is needed.
Good Luck,
Bill


Kenosis
User

Jan 5, 2014, 12:43 PM

Post #7 of 8 (890 views)
Re: [zapzap] need some help to write a simple script [In reply to] Can't Post


Quote
I do not really care if this is a homework assignment or not. If you need help in a perl homework assignment then this is the place.

In fact I find it highly illogical to shun individuals with homework ...

You haven't helped the OP at all, since doing all of the OP's work for him/her isn't help. BillKSmith sagely suggested a form of help by providing a "hint." And after you clobbered the OP over the head with a large fish when s/he asked for only help on how to fish ("I need help to write a script..."), how meaningful could your code be to the OP when it's clearly evident that there are emergent programming skills issues here? Let me make this last point abundantly clear from a pedagogical/developmental perspective: if the OP can understand your code, the OP could have solved the problem in the first place.

Also, how much 'help' are you really offering now, almost three weeks after the OP's post? The class is likely finished; most certainly the assignment's past due. Were you not aware that an implicit solution to the OP's request was already provided here--two days after the OP's posting? All that the OP needed to do was change

Code
<DATA>


to

Code
<>


remove the

Code
__DATA__

and its lines, and simply invoke the script by:

Code
perl script.pl inFile >P1.half

Finally, who 'shunned' an individual with homework? Are you seriously calling a question to the OP about whether the problem is homework an act of shunning? The question to the OP is important for at least two reasons: 1) it directs the OP's attention to their responsibility qua student, and 2) it helps respondents from becoming mere enablers to those seeking an easy way out.


(This post was edited by Kenosis on Jan 5, 2014, 3:45 PM)


Laurent_R
Enthusiast / Moderator

Jan 6, 2014, 12:01 PM

Post #8 of 8 (795 views)
Re: [zapzap] need some help to write a simple script [In reply to] Can't Post


In Reply To
Greetings.

I do not really care if this is a homework assignment or not. If you need help in a perl homework assignment then this is the place.


Nope, I completely disagree. There is a general agreement on Perl and other programming forums that you may help a student not able to solve his/her assignment, say by providing a hint or some corrections to posted code, but that you should not do the home work for him/her, as this is counterproductive.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives