0

I'm doing an Array that contents each word of a phrase. When I try to split it and print the length then the console gives me an enormous number such as 111039391231319239188238139123919232913123... (more lines) why?

Here's my code:

$mynames = $texto3;
print $mynames. "\n";
@nameList = split(' ', $texto3);
#print @nameList.length();
for ($to = 0; $to<@nameList.length; $to++){
        if($to<@nameList.length) {
                @nameList[$to] = @nameList[$to] . "_" . @nameList[$to++];
         }
         print $to;
         #print @nameList[$to] . "\n";
 }
 $string_level2 = join(' ', @nameList);
 #print $string_level2;
Marc Ortiz
  • 2,242
  • 5
  • 27
  • 46
  • 1
    your `for` loop appears to have a bug in it. I believe that you meant to have used a `<` rather than a `>` – levengli Jul 09 '13 at 12:36
  • 1
    What language is this? Doesn't look much like Perl to me. Why do you expect `split( ' ' ...` to work? And who gave you the idea that `@nameList.length` is valid syntax? Yes, Perl is flexible, but it cannot read your mind and you cannot just make up the syntax as you go and expect it to work. – innaM Jul 09 '13 at 12:48
  • It could be Perl 6, sort of... :-) – Slaven Rezic Jul 09 '13 at 13:09
  • Try: `print "$to ";` And split with a space character rather than a match, performs an `awk` split. See http://perldoc.perl.org/functions/split.html And `@nameList.length` is valid; it means string concatenate the number of items in @nameList to the length of the string $_. – shawnhcorey Jul 09 '13 at 14:08
  • 3
    Did no-one ever tell you to add `use strict` and `use warnings` to the top of all of your Perl programs? – Dave Cross Jul 09 '13 at 14:22

1 Answers1

3

To get the length of an array use scalar @nameList instead of @nameList.length.

A typical for-loop uses the less-than operator when counting up, e.g.:

for ( $to = 0; $to < scalar(@nameList); $to++ ) ...

You should never use a post-increment unless you understand the side effects. I believe the following line:

@nameList[$to] = @nameList[$to] . "_" . @nameList[$to++];

... should be written as ...


    $nameList[$to] = $nameList[$to] . "_" . $nameList[$to + 1];

Finally the comparison you use should account for the boundary condition (because you refer to $to + 1 inside the loop):

if( $to < (scalar(@nameList) - 1) ) {
  $nameList[ $to ] = $nameList[ $to ] . "_" . $nameList[ $to + 1 ];
}
PP.
  • 10,764
  • 7
  • 45
  • 59
  • 1
    Moreover, in numeric comparison, the conversion to scalar is implicit, so you can use simply `$to < @nameList`. – choroba Jul 09 '13 at 12:39
  • 2
    @choroba I agree - but in this case the user is so novice that they are safer to explicitly code to prove to themselves they understand what they are doing. Shortcuts can come later. – PP. Jul 09 '13 at 12:41
  • 2
    Please don't use the three argument `for`. Use `foreach` instead: `for my $to ( 0 .. $#nameList ){` – shawnhcorey Jul 09 '13 at 14:09
  • Please use the three argument list. This is Perl. There are many ways to do it. Remember that. The list version of for is generally faster for arrays, yes. But you needn't be bound to do this always. [Golden Hammer](http://xkcd.com/801/) is not a recommended approach in programming or life. – PP. Jul 09 '13 at 14:38