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:
hi i have a test and i need your help

 



gal-x-e
Novice

Dec 19, 2013, 1:49 PM

Post #1 of 22 (2705 views)
hi i have a test and i need your help Can't Post

Thanks to all the helpers
How can i Write a program in
Perl
 On behalf of
P1
 Accepts a file name as a parameter and creates a file name as the file name extension
half
, From program to program original file after replacing the double length of each word in the first half of the word. (On the lines of the file structure remain unchanged). Can assume that exactly one space between words in a file.

For example suppose the file content
F1
 Is:
 
aab ccccf 123A
ccc ccc wwwwww 123a 61234 77
88888 uuuu 2 uuuuu ccccf
 
       Then, after the reading program
P1 F1
 Created a file named
F1.half
 And its content is:
 
aab ccccf 12
www ccc ccc 12 61 234 7
88888 uu 2 uuuuu ccccf

Again thanks for the help


gal-x-e
Novice

Dec 19, 2013, 10:10 PM

Post #2 of 22 (2696 views)
Re: [gal-x-e] hi i have a test and i need your help [In reply to] Can't Post


In Reply To
Thanks to all the helpers
How can i Write a program in
Perl
 On behalf of
P1
 Accepts a file name as a parameter and creates a file name as the file name extension
half
, From program to program original file after replacing the double length of each word in the first half of the word. (On the lines of the file structure remain unchanged). Can assume that exactly one space between words in a file.

For example suppose the file content
F1
 Is:
 
aab ccccf 123A
ccc ccc wwwwww 123a 61234 77
88888 uuuu 2 uuuuu ccccf
 
       Then, after the reading program
P1 F1
 Created a file named
F1.half
 And its content is:
 
aab ccccf 12
www ccc ccc 12 61 234 7
88888 uu 2 uuuuu ccccf

Again thanks for the help


Please, it is very urgent


Kenosis
User

Dec 19, 2013, 10:41 PM

Post #3 of 22 (2693 views)
Re: [gal-x-e] hi i have a test and i need your help [In reply to] Can't Post

It's evident that these are exercises.

What have you tried and how has it failed?


gal-x-e
Novice

Dec 20, 2013, 1:20 AM

Post #4 of 22 (2683 views)
Re: [Kenosis] hi i have a test and i need your help [In reply to] Can't Post

In fact, I'm trying to get hired at the Department of UNIX, they teach us everything from the beginning, they only examine my ability to cope, with the test.
And I just started messing around with PERL, yet I have nothing really
Thanks again


gal-x-e
Novice

Dec 20, 2013, 3:51 AM

Post #5 of 22 (2677 views)
Re: [Kenosis] hi i have a test and i need your help [In reply to] Can't Post

so far i have

#!/usr/bin/perl

use strict;
use warnings;

print "Hello tester ";
$words = <STDIN> \n;


BillKSmith
Veteran

Dec 20, 2013, 7:12 AM

Post #6 of 22 (2668 views)
Re: [gal-x-e] hi i have a test and i need your help [In reply to] Can't Post

You really have two problems. The first is to get the names of the input and output files and open them. The second is to create the contents of the output file from the contents of the input file.

You call the input file name a 'parameter'. I assume that this means an option specified on the command line. Refer to the documentation on the special variable @ARGV in perldoc perlvar. You do not need any input form STDIN.

Form the output file name by using a regex to replace the extension of the input file name.


I really do not understand your processing requirements.
Good Luck,
Bill


gal-x-e
Novice

Dec 20, 2013, 7:20 AM

Post #7 of 22 (2666 views)
Re: [BillKSmith] hi i have a test and i need your help [In reply to] Can't Post

For example suppose the file content
F1
Is:

aab ccccf 123A
ccc ccc wwwwww 123a 61234 77
88888 uuuu 2 uuuuu ccccf

Then, after the reading program
P1 F1
Created a file named
F1.half
And its content is:

aab ccccf 12
ccc ccc www 12 61234 7
88888 uu 2 uuuuu ccccf

Again thanks for the help


BillKSmith
Veteran

Dec 20, 2013, 8:35 AM

Post #8 of 22 (2662 views)
Re: [gal-x-e] hi i have a test and i need your help [In reply to] Can't Post

It seems that you are only interested in 'words' which have an even number of characters. If the second half of the word is the same as the first, delete the second half. This statement of the problem can be implemented easily.
Good Luck,
Bill


gal-x-e
Novice

Dec 20, 2013, 12:02 PM

Post #9 of 22 (2653 views)
Re: [BillKSmith] hi i have a test and i need your help [In reply to] Can't Post

but it is also hve to be a ver that "/2". anly then delet the 2 half.
i also don't know what is the code for the create the folder. p1.half


2teez
Novice

Dec 20, 2013, 12:57 PM

Post #10 of 22 (2647 views)
Re: [gal-x-e] hi i have a test and i need your help [In reply to] Can't Post

Hi,
I believe your question has been answered larger by previous answers, especially by BillkSmith.
However, to demonstrate how you can go about it, this is one way:

Code
use warnings; 
use strict;

while ( my $line = <DATA> ) {
## intialize the final line to print
my $line_to_print = q{};
## split each line on space
for ( split /\s+/ => $line ) {
## split each words into letters
my @arr = split // => $_;
$line_to_print .=
( @arr % 2 ) != 0
? join( '' => @arr ) . q{ }
: join( '' => substr $_, 0, ( @arr / 2 ) ) . q{ };
}
print $line_to_print, $/; ## print the new line
}

__DATA__
aab ccccf 123A
ccc ccc wwwwww 123a 61234 77
88888 uuuu 2 uuuuu ccccf


You should have:

Code
aab ccccf 12  
ccc ccc www 12 61234 7
88888 uu 2 uuuuu ccccf


Of course, instead of using the ternary Operators (?:) like I did, you could use if/else.


(This post was edited by 2teez on Dec 20, 2013, 1:01 PM)


BillKSmith
Veteran

Dec 20, 2013, 1:09 PM

Post #11 of 22 (2644 views)
Re: [2teez] hi i have a test and i need your help [In reply to] Can't Post

All the processing can be done with a single regex:


Code
$line =~ s/\b(\S+)\1\b/$1/g;

Good Luck,
Bill


2teez
Novice

Dec 20, 2013, 1:42 PM

Post #12 of 22 (2640 views)
Re: [BillKSmith] hi i have a test and i need your help [In reply to] Can't Post


Quote
All the processing can be done with a single regex:

Hummm.. maybe, maybe not!
Check the output, using the regex. It quite different to the output specified by the OP. I think the OP should go check it out.


Kenosis
User

Dec 20, 2013, 4:12 PM

Post #13 of 22 (2622 views)
Re: [gal-x-e] hi i have a test and i need your help [In reply to] Can't Post

Perhaps the following will be helpful:


Code
use strict; 
use warnings;

while (<DATA>) {
s!(\w+)!my $len = length $1; $len % 2 ? $1 : substr $1, 0, $len/2!ge;
print;
}

__DATA__
aab ccccf 123A
ccc ccc wwwwww 123a 61234 77
88888 uuuu 2 uuuuu ccccf


Output:


Code
aab ccccf 12   
ccc ccc www 12 61234 7
88888 uu 2 uuuuu ccccf



(This post was edited by Kenosis on Dec 20, 2013, 9:40 PM)


BillKSmith
Veteran

Dec 20, 2013, 7:14 PM

Post #14 of 22 (2603 views)
Re: [2teez] hi i have a test and i need your help [In reply to] Can't Post

You are certainly right about the need for OP testing. My regex implements my interpretation (post #8) of the spec. I do not understand the OP's critique of it.
Good Luck,
Bill


gal-x-e
Novice

Dec 21, 2013, 2:19 AM

Post #15 of 22 (2570 views)
Re: [Kenosis] hi i have a test and i need your help [In reply to] Can't Post

will it work or anny kind of typing
for example:

hdkjashdkjah fdsf fff rreer
hssde fdfkj fsfs feeeaaf fvveea

the output will be:
hdkjas fd fff rreer
hssde fdfkj fs feeeaaf fvv




and anoter tesk:

Write a program in Perl called P2 receives as parameters a list of files ( of any length ) and prints output one line for each file that contains file name , followed by one space , followed by a number that contains the number of different words in a file. The order of rows to be by file names wrong Llachsicografi costs .
In addition, the program prints the last output line list of file names that the number of different words in them is greatest. File names last output line to be separated by one space and sorted fine Llachsicografi costs .

For example, suppose the following data files whose content is :

   A B C K E
a a b ab ab ab 20 xyza abcd 1
b b a 5 4 6 200 xyz 2
                                         3
                                 4

After running the program by the command :
P2 A B C K E
         Received output :

A 2
B 4
C 4
E 4
K 1
B C E


(This post was edited by gal-x-e on Dec 21, 2013, 6:01 AM)


BillKSmith
Veteran

Dec 21, 2013, 6:11 AM

Post #16 of 22 (2543 views)
Re: [gal-x-e] hi i have a test and i need your help [In reply to] Can't Post

My regex passes all of your previous test and several that I made up. It fails both of the new ones ('OP 4' and 'OP 5' below).


Code
use strict; 
use warnings;
use Test::Simple tests => 9;
my $RE_UNDER_TEST = qr/\b (\S+) \1 \b/x;

my @cases = (
# input expected output description
['aab ccccf 123A', 'aab ccccf 123A', 'OP 1' ],
['ccc ccc wwwwww 123a 61234 7', 'ccc ccc www 123a 61234 7', 'OP 2' ],
['88888 uuuu 2 uuuuu ccccf', '88888 uu 2 uuuuu ccccf', 'OP 3' ],
['hdkjashdkjah fdsf fff rreer', 'hdkjas fd fff rreer', 'OP 4' ],
['hssde fdfkj fsfs feeeaaf fvveea ',
'hssde fdfkj fs feeeaaf fvv','OP 5' ],
['a abcabc xxx uuuuu cccc f', 'a abc xxx uuuuu cc f', 'Two Match'],
['xyzabcxyzabc 2 uuu g', 'xyzabc 2 uuu g', 'Start' ],
['xyzabc1xyzabc 2 uuu g', 'xyzabc1xyzabc 2 uuu g', 'No Match' ],
['88888 3 uuuu', '88888 3 uu', 'End' ],
);

foreach my $case (@cases) {
(my $out = $$case[0]) =~ s/$RE_UNDER_TEST/$1/g;
my $expected = $$case[1];
my $description = $$case[2];
ok($out eq $expected, $description);
};


Please check your examples. If they are correct, explain your expected results.
Good Luck,
Bill


gal-x-e
Novice

Dec 21, 2013, 12:12 PM

Post #17 of 22 (2518 views)
Re: [gal-x-e] hi i have a test and i need your help [In reply to] Can't Post

Someone?

By the way thanks for all the help so far


2teez
Novice

Dec 21, 2013, 12:35 PM

Post #18 of 22 (2516 views)
Re: [gal-x-e] hi i have a test and i need your help [In reply to] Can't Post


Quote
will it work or anny kind of typing

Why don't you try it? If am not mistaken there are about 3 different solutions to your question, try your hands on them. Please try them all out and see for yourself. That is how one learns.


Quote
and anoter tesk:

Of course, the saying is right, that don't give a man a fish, teach him how to catch one. I don't think you will get very far learning to program if one has to do all your task for you. Even if it's practicing.
Please show some effort, then others help and guidance will make you better. Why must other do all your work for you?


gal-x-e
Novice

Dec 21, 2013, 12:45 PM

Post #19 of 22 (2513 views)
Re: [2teez] hi i have a test and i need your help [In reply to] Can't Post

You're absolutely right
I'm really not looking for a free lunch
The thing is that I only need to prove myself, to applying for a job
Then they teach from 0
And I really need this job

Until now I got great help
Of course I'm still learning on YouTube
There are still some things that I found and I do not have time to search

I did not understand how to create a folder
How to call it a specific name


2teez
Novice

Dec 21, 2013, 1:15 PM

Post #20 of 22 (2507 views)
Re: [gal-x-e] hi i have a test and i need your help [In reply to] Can't Post


Quote
There are still some things that I found and I do not have time to search

Then maybe you need to check Perl FAQ:http://perldoc.perl.org/perlfaq.html OR
you can do

Code
perldoc perlfaq

on your command Line Interface.


Quote
I did not understand how to create a folder
How to call it a specific name

This Perl command works on *nix and window OS

Code
perl -we 'system"mkdir gal-x-e"'

It makes a folder with the name gal-x-e.
Note that on win OS you use double quote!! Something like this:

Code
perl -we "system qq(mkdir gal-x-e)"



(This post was edited by 2teez on Dec 21, 2013, 1:21 PM)


FishMonger
Veteran / Moderator

Dec 21, 2013, 2:23 PM

Post #21 of 22 (2499 views)
Re: [2teez] hi i have a test and i need your help [In reply to] Can't Post

No need to make a system call to create a directory. Perl has a built-in mkdir function.

http://perldoc.perl.org/functions/mkdir.html


2teez
Novice

Dec 21, 2013, 2:42 PM

Post #22 of 22 (2496 views)
Re: [FishMonger] hi i have a test and i need your help [In reply to] Can't Post

Thanks FishMonger. I wonder what I was thinking.
Maybe staying too much on this is *nix box is eroding my mind!


Code
perldoc -f mkdir

gives you all the info. you want.

 
 


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

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