Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: not able to get the output using split function

by CountZero (Bishop)
on Sep 02, 2012 at 17:39 UTC ( #991313=note: print w/ replies, xml ) Need Help??

in reply to not able to get the output using split function

That ungainly "C-style" for loop, can be made much more perlish as follows:

use Modern::Perl '2011'; my $sentence = 'aaaa bbb ccc'; my @words = split(/\s+/, $sentence); my $i; while (my ($i, $word) = each @words) { say "$i: $word"; }
Note: it will need at least Perl 5.012 to work.


A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics

Comment on Re: not able to get the output using split function
Select or Download Code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://991313]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2015-11-30 01:52 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (756 votes), past polls