Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

not able to get the output using split function

by ulaksh (Acolyte)
on Sep 01, 2012 at 21:25 UTC ( #991195=perlquestion: print w/ replies, xml ) Need Help??
ulaksh has asked for the wisdom of the Perl Monks concerning the following question:

I am not able to get the output using split function and I am not able to understand where is the mistake.
use strict; use warnings; my $sentence = shift; my @words = split(/\s+/, $sentence); my $i; for($i=0;$i<scalar (@words);$i++) { print "$i: " . $words[$i] . "\n"; }
My output shows as below.
0: i

Comment on not able to get the output using split function
Select or Download Code
Re: not able to get the output using split function
by philiprbrenan (Monk) on Sep 01, 2012 at 21:35 UTC

    Your code is fine, but $sentence has no value.

    use feature ":5.14"; use warnings FATAL => qw(all); use strict; use Data::Dump qw(dump pp); my $sentence = 'aaaa bbb ccc'; my @words = split(/\s+/, $sentence); my $i; for($i=0;$i<scalar (@words);$i++) { print "$i: " . $words[$i] . "\n"; }

    Produces

    0: aaaa 1: bbb 2: ccc
Re: not able to get the output using split function
by NetWallah (Abbot) on Sep 01, 2012 at 22:09 UTC
    Your code:
    my $sentence = shift;
    will pick up the FIRST argument passed-in.

    Your test probably looked something like:

    >perl myprogram i amd testing this.
    and it picked-up just the "i".

    There are two possible fixes:
    1. Add quotes to the argument, like this:

    >perl myprogram "i amd testing this."
    2. Programattically capture ALL passd arguments:
    my $sentence = join " ",@ARGV;
    If you notice, @ARGV already contains the same information as @words for this case, so split is unnecessary.

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

Re: not able to get the output using split function
by AnomalousMonk (Monsignor) on Sep 02, 2012 at 07:44 UTC

    ulaksh:
    Two points spring to mind. First, in your OP you do not show the input, thus forcing NetWallah to guess in his reply that maybe you had not properly quoted the command-line argument(s). Please see How do I post a question effectively?.

    Second (and far worse, IMHO), you forced yourself to guess at the input by not including some fairly simple debug statements in your code. A few well-placed print statements or array or hash dump statements can bring blessed enlightenment. (There is also the integrated Perl debugger, see perldebug, but that takes a little effort to learn and many programmers go through their entire careers doing little more than sprinkling some debug print statements strategically through their code.)

    >perl -wMstrict -le "use Data::Dump; ;; my $sentence = shift; print qq{shifted: '$sentence'}; ;; my @words = split(/\s+/, $sentence); dd \@words; ;; my $i; for($i=0;$i<scalar (@words);$i++) { print qq{$i: $words[$i]}; } " the quick brown fox shifted: 'the' ["the"] 0: the
Re: not able to get the output using split function
by CountZero (Bishop) on Sep 02, 2012 at 17:39 UTC
    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
Re: not able to get the output using split function
by GrandFather (Cardinal) on Sep 03, 2012 at 05:09 UTC
    my $i; for ($i = 0; $i < scalar(@words); $i++) { print "$i: " . $words[$i] . "\n"; }

    is better written:

    for my $i (0 .. $#words) { print "$i: $words[$i]\n"; }

    or could be written using for as a statement modifier as:

    print "$_: $words[$_]\n" for 0 .. $#words;
    True laziness is hard work

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2014-08-22 18:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (163 votes), past polls