sqspat has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am trying to split an array and when I do and I try to print the first element of the array, it is returning the number of lines in the array and not the element, see code below;
42 my @command_array = <FILE>; 43 close(FILE); 44 45 print "@command_array\n"; 46 my @command_array_split = split(/:/,@command_array); 47 48 49 print "Individual Elements\n\n"; 50 print "@command_array_split[0]\n"; 51 print "@command_array_split[1]\n";
Code is returning ;
fault-reportin email smtp-server 1.2.3.4 from me@here.com recipient me1@here.com recipient me2@here.com recipient me3@here.com recipient me4@here.com enable Individual elements 7 ---- Why is this coming back and not the value of the element--- + @command_array_split[0]
How do I print the array elements and not the # of lines in the array?

Replies are listed 'Best First'.
Re: problem printing array elements -- printing # of lines in array instead
by davorg (Chancellor) on Jul 13, 2009 at 13:42 UTC

    It all goes wrong on your line 46. As you'll see from the documentation, the second argument to split is a scalar value. Therefore your array is evaluated in scalar context and returns the number of elements in the array.

    It's also (as always) worth using strict and warnings. In this case, you'll see warnings like "Scalar value @command_array_split[0] better written as $command_array_split[0]".

    --

    See the Copyright notice on my home node.

    Perl training courses

Re: problem printing array elements -- printing # of lines in array instead
by moritz (Cardinal) on Jul 13, 2009 at 13:41 UTC
    @command_array is an array, and you're using it in split where a scalar is expected.

    An array in scalar context returns the number items.

    You can print all items with print @command_array or by iterating over the values, and printing them one by one. Depends on what you want to achieve in the end.

Re: problem printing array elements -- printing # of lines in array instead
by Corion (Patriarch) on Jul 13, 2009 at 13:39 UTC
    my @command_array_split = split(/:/,@command_array);

    This does not do what you think. split does not take an array or a list.

Re: problem printing array elements -- printing # of lines in array instead
by Marshall (Canon) on Jul 13, 2009 at 14:54 UTC
    I am not sure that your data looks like, but consider this:
    while (<FILE>) { chomp; my ($first_thing, @cmds) = split(/:/,$_); print "$first_thing\n"; print "Individual Elements\n\n"; foreach my $cmd (@cmds) { print " $cmd\n"; } }
    Update: don't "slurp" a file into a @var unless you need to do that.
    process line by line instead.
    I think Corion already pointed out that split works on a scalar, not a @var
Re: problem printing array elements -- printing # of lines in array instead
by biohisham (Priest) on Jul 13, 2009 at 15:59 UTC
    you have to remember that you split a a string into discrete elements that would be fed to an array and you join discrete elements together from an array to form a continuous string, hence you can not do what you have been trying (i.e to split an array) unless of course you are using that array in a different context by dereferencing it with $array[$index]...another thing is that, from how you printed @command_array at the beginning of the program I wonder if you used the appropriate splitting pattern, you are splitting over /:/, so I am not sure what your data looks like and remember bad data can ruin your day, so if you want to split around /:/ this pattern better be sure your data elements are separated according to this pattern, I have made the DATA part into a one-line : separated...I have come up with this code, it solves the problem if your data was organized that way.
    #!/usr/local/bin/perl use strict; use warnings; my $index; my @command_array = <DATA>; my @command_array_split; print "@command_array"; for($index=0;$index<=$#command_array;$index++){ my @command_array_split = push @command_array_split,(split(/:/ +,$command_array[$index])); } print "Individual Elements\n\n"; print "$command_array_split[0]\n"; print "$command_array_split[1]\n"; print "$command_array_split[3]\n"; __DATA__ fault-reportin email:smtp-server 1.2.3.4 from me@here.com:recipient me +1@here.com:recipient me2@here.com:recipient me3@here.com:recipient me +4@here.com:enable
    I welcome everyone to criticize my programming approach, for I am not perfect and towards perfection I am sailing comrades.