Home: Perl Programming Help: Intermediate:
Perl operator 'x'



rajshah13
New User

Nov 19, 2012, 5:55 PM


Views: 4043
Perl operator 'x'

how can i produce and print the line: '666:666:666:god help us!'
without printing any number 6 and with only one : with perl script ?


FishMonger
Veteran / Moderator

Nov 20, 2012, 6:41 AM


Views: 4041
Re: [rajshah13] Perl operator 'x'

See: perldoc -f join
http://perldoc.perl.org/functions/join.html


Laurent_R
Veteran / Moderator

Nov 20, 2012, 10:16 AM


Views: 4037
Re: [rajshah13] Perl operator 'x'

An example not even using 6:


Code
my $nb = 777 - 111; 
my $line = $nb . ":";
$line = $line x 3;
$line = $line . "God";


;-)


(This post was edited by Laurent_R on Nov 20, 2012, 10:32 AM)


FishMonger
Veteran / Moderator

Nov 20, 2012, 10:47 AM


Views: 4032
Re: [Laurent_R] Perl operator 'x'

I was holding off providing a solution because this is clearly a homework assignment where the OP hasn't shown any attempt.

Since "the cat's out of the bag", I'll show one anyway.


Code
print join ':', (555+111) x 3, 'god help us!';



(This post was edited by FishMonger on Nov 20, 2012, 10:48 AM)


Laurent_R
Veteran / Moderator

Nov 20, 2012, 2:06 PM


Views: 4023
Re: [FishMonger] Perl operator 'x'

Yeah, I did not think about possible homework, but now that you say it, it is fairly clear.

At the same time, the solution I suggested does work, but was not meant to be very serious, but more as a clue on how it could be done. I realize now that it can be used as an answer to an assignment, even though there could be better ones. For one, the x operator could be better used, but I won't say more.


rovf
Veteran

Nov 21, 2012, 1:31 AM


Views: 4009
Re: [FishMonger] Perl operator 'x'

This doesn't fulfil the OPs specification. Note that the problem spec says:


Quote
... without printing any number 6


Your code doesn't use the number 6, but certainly prints it.

I think it is very hard to print 666 without printing a 6. Maybe a solution would be to do a print 999 and ask the user to turn the monitor upside down....


FishMonger
Veteran / Moderator

Nov 21, 2012, 8:17 AM


Views: 4002
Re: [rovf] Perl operator 'x'


Quote
I think it is very hard to print 666 without printing a 6. Maybe a solution would be to do a print 999 and ask the user to turn the monitor upside down....


I like that idea. :o)


Laurent_R
Veteran / Moderator

Nov 21, 2012, 10:18 AM


Views: 3998
Re: [rovf] Perl operator 'x'

Well, yes, I noticed that, but I took it to mean without using 6 in the program. It does not make much sense otherwise.