Range operator and odd numbers

R

Roger

Greetings...

I have an array that is populated with odd numbers.

@odd_num = ("1","3","5","7","9","11","13","15","17","19","21",
"23","25","27","29","31","33","35","37","39","41","43");

I would like to clean up this code and use the range operator '..' and I was
seeking a clever way to get a set of odd numbers like above. AFAIK the range
operator returns whole numbers.

Thanks for any insight.
 
R

Richard Gration

"Roger" <[email protected]> said:
Greetings...
I have an array that is populated with odd numbers. @odd_num =
("1","3","5","7","9","11","13","15","17","19","21",
"23","25","27","29","31","33","35","37","39","41","43"); I would like
to clean up this code and use the range operator '..' and I was seeking
a clever way to get a set of odd numbers like above. AFAIK the range
operator returns whole numbers.
Thanks for any insight.

here's a couple

@odd_num = map {2 * $_ + 1} (0 .. 21)

@odd_num = grep {$_ % 2} (1..43)

HTH :)

Rich
 
R

Roger

@odd_num = map {2 * $_ + 1} (0 .. 21)

@odd_num = grep {$_ % 2} (1..43)

That's TOO COOL!!! Thanks a bunch, it's things like that , that help me learn
perl better, real life examples!!
The second one with grep is awesome!

:))
Rodger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,408
Latest member
AlenaRay88

Latest Threads

Top