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

I know that is a VERY basic question, but I am working on it since this morning and it is driving me made. But since "the more time you spend on a problem, the more stupid is this problem", I realise the solution might be very simple.

Here is the code of my cgi script :

#!usr/bin/perl use CGI; $res = "aaaaubbbbuooooo"; @rest = split /u/,$res; $restLength = length(@rest); print "Content-type: text/html\n\n"; print "restLength : $restLength"; for ($i=0;$i<length(@rest);$i++){ print "@rest[$i]"; }

My browser print :
restLength : 1
aaaa

Why does it not print :
restLength : 3
aaaa
bbbb
ooooo

Why why why why ????

thx

janitored by ybiC: Balanced <code> tags, minor format cleanup

Replies are listed 'Best First'.
Re: Basic question about split
by Enlil (Parson) on Dec 12, 2003 at 23:29 UTC
    perldoc length states:

    Returns the length in characters of the value of EXPR. If EXPR is omitted, returns length of $_. Note that this cannot be used on an entire array or hash to find out how many elements these have. For that, use scalar @array and scalar keys %hash respectively.

    So when you are doing

    $restLength = length(@rest);
    I think you mean (the scalar function being optional in this case):
    $restLength = scalar @rest;

    On another note there is not need to go through all the contortion of making your loop c-like. Something like:

    for my $item (@rest) { print "$item": }
    will work just as well, and relieve you of worrying about iterators.

    update: I neglected to mention that your split is working fine, but that it was the length bit that was causing what you were observing.

    -enlil

      Interestingly enough, in this Apocalypse lwall basically says that length(@array) should be more DWIMmy, and that it more or less will be in perl6.


      Who is Kayser Söze?
      Code is (almost) always untested.
Re: Basic question about split
by Anonymous Monk on Dec 12, 2003 at 23:32 UTC

    Try like this:

    #!usr/bin/perl -w use strict; use CGI; my $res = "aaaaubbbbuooooo"; my @rest = split /u/,$res; my $restLength = @rest; print "Content-type: text/html\n\n"; print "restLength : $restLength\n"; for (my $i=0;$i< @rest;$i++){ print "@rest[$i]\n"; } __END__ restLength : 3 aaaa bbbb ooooo

    length doesn't do what you want. You need scalar @array to get the size of an array.

      Works :-)
      Thanks a lot.
      I still have to understand both perl and how this forum works. When I go to the index of "Seekers of Perl Wisdom" I do not see my question. I used the "search method to find it and to see your answers. In the meantime I wrote my question again. Sorry to everybody for the mess.

        That explains the similar questions :)

        The way this forum works is that certain questions posted don't appear in Seekers Of Perl Wisdom right away. They have to be "approved" by uber-monks first. It will still appear under "Newest Nodes" so there really isn't a reason to post twice.

        There is some reading info here: MonkStart

Re: basic question on split
by simonm (Vicar) on Dec 12, 2003 at 23:37 UTC
    You'll need to replace length(@rest) with scalar(@rest); length is for strings only, not arrays.
Re: Basic question about split
by Zaxo (Archbishop) on Dec 12, 2003 at 23:41 UTC

    I think you want my $restLength = @rest; It's not a split problem at all. The length function puts @rest in scalar context, giving 3, and you're getting length('3').

    After Compline,
    Zaxo

Re: basic question on split
by flyingmoose (Priest) on Dec 12, 2003 at 23:59 UTC
    The above posters are, of course, right. I would most likely do it this way, however...
    foreach $elem (@rest) { print $elem; }
Re: basic question on split
by CountZero (Bishop) on Dec 13, 2003 at 10:42 UTC
    On a more general issue, I think that you need to read up some more on the use of variables, arrays and hashes in perl.

    The answer to your question and some good tutorial on variables, ... can be found on this off-site link.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: basic question on split
by Nkuvu (Priest) on Dec 12, 2003 at 23:42 UTC

    Off topic question. You use CGI then print headers manually?

    Semi-on-topic question. Why not use the $restLength variable in the for loop so you're not calling the length function (or the scalar function once you correct your code as simonm pointed out) over and over again?

    Another off topic observation. print "@rest[$i]" probably isn't what you want. That makes a slice. You most likely want to get into the habit of using code more like print "$rest[$i]"