Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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.

CountZero

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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2014-09-18 23:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (126 votes), past polls