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:
grep and split

 



Nitric
New User

Jul 7, 2016, 11:00 PM

Post #1 of 5 (1913 views)
grep and split Can't Post

Please help with correcting syntax

#!/usr/bin/perl
# your code goes here

use strict;
use warnings;
my $i;
my $j = 'tt@b(defgh|jk3m#9pqr$+uvwxy2tt';

for ($i=0;$i<32;++$i) {
print (grep{/\S/}split('\W|',$j)[)$i%(split('',$j))[11]+$i%(split('',$j))[25]*$i%19];
}
print "\n";
;


Laurent_R
Veteran / Moderator

Jul 7, 2016, 11:18 PM

Post #2 of 5 (1911 views)
Re: [Nitric] grep and split [In reply to] Can't Post

Please tell us:
- in which respect you code doesn't do what you expect;
- what do you expect it to do (perhaps show sample input and expected output).

Please also repost your code within code tags, with appropriate spacing and indentation, so that we don't have to decipher it character by character to understand the intent.


(This post was edited by Laurent_R on Jul 7, 2016, 11:20 PM)


Nitric
New User

Jul 7, 2016, 11:21 PM

Post #3 of 5 (1908 views)
Re: [Laurent_R] grep and split [In reply to] Can't Post


Code
    #!/usr/bin/perl 
# your code goes here

use strict;
use warnings;
my $i;
my $j = 'tt@b(defgh|jk3m#9pqr$+uvwxy2tt';

for ($i=0;$i<32;++$i) {
print (grep{/\S/}split('\W|',$j))[$i%(split('',$j))[11]+$i%(split('',$j))[25]*$i%19];
}
print "\n";
;


It has given in task for me, I must correct the code and take an secret word


Nitric
New User

Jul 7, 2016, 11:23 PM

Post #4 of 5 (1907 views)
Re: [Nitric] grep and split [In reply to] Can't Post

There is some changes in stroke 10, print function. I must correct them and get a answer of programm.


BillKSmith
Veteran

Jul 8, 2016, 6:01 AM

Post #5 of 5 (1904 views)
Re: [Nitric] grep and split [In reply to] Can't Post

Your long print statement does not compile. The error message that you receive refers to the following substring from that statement: ")[)$i". It tells you that an operator is required between the right parenthesis and the $i. Unfortunately for you, perl gives up and reports this error before it has a chance to discover that this combination of parenthesis does not make any sense.

I cannot fix it for you because I cannot begin to guess what it is supposed to do. Good practice dictates breaking this complex statement into several simpler ones. Debugging would be much easier. Please post either corrected code or a clear description of your objective.

Even after you fix your syntax, your program will still not work as you intend. The first argument of split is a regular expression. You may code it as a string, but regex syntax still applies. The pipe "|" character has special meaning in a regex. It must be escaped to represent itself.

I could make several comments on your style, but lets get this version working first.
Good Luck,
Bill

 
 


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

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