strfry() has asked for the wisdom of the Perl Monks concerning the following question:

hmm, have a (hopefully (: ) quick question: how would i return the result of a for() loop within a subroutine? example:
sub scroll { my ($i) = @_; my @x = for (my $x=0;$x!=(length($i)+1);$x++){ substr($i,$x,1), "\n"; +}; return @x; }

now, this doesn't work, and spews forth quite a few warnings and errors (: but how would i go about returning the value?
it might be helpful to note that when i write it as:
sub scroll { my ($i) = @_ for (my $x=0;$x!=(length($i)+1);$x++){ print substr($i,$x,1), "\n"; } }
it works.

strfry()

Replies are listed 'Best First'.
Re: returning data from a for() loop?
by bikeNomad (Priest) on Jun 08, 2001 at 23:19 UTC
    For loops don't have result values.

    If you want to return an array from a loop, either accumulate it, or use a function that returns one (map, grep, split, etc.). Your scroll() routine could be better written as something like:

    sub scroll { map { $_, "\n" } split('', shift) }
    if you're trying to return an array that alternates between input characters and newlines, or
    sub scroll { map { "$_\n" } split('', shift) }
    if you're trying to return an array of strings that have successive characters from the argument ending with newlines. It wasn't clear which you wanted.

    update: added clarification

Re: returning data from a for() loop?
by srawls (Friar) on Jun 08, 2001 at 23:14 UTC
    How 'bout this:
    push @x,substr($i,$_,1) for(0 .. length $i);

    Update:Or this (uses map):

    @x = map{substr($i,$_,1)}0 .. length $i;


    The 15 year old, freshman programmer,
    Stephen Rawls
      the first one worked just fine, with a tiny tweak:
      #!/usr/bin/perl -w use strict; my $hi = "flux"; print &scroll($hi); sub scroll { my ($i) = @_; my @x; push @x,substr($i,$_,1),"\n" for (0 .. length $i); return @x; }
      (:
      strfry()
Re: returning data from a for() loop?
by Sifmole (Chaplain) on Jun 08, 2001 at 23:13 UTC
    I do not believe that "for"s return a result.

    Are you trying to split the string into individual characters? Then you should read up on "split".
    Are you trying to manipulate each element of an array and hold onto the manipulation? Then you should read up on "map".

      i'm trying to split them into individual characters with an arbitrary character inbetween each one (in this case, either "\n" or <br>
      i did try to read up on map, but i need simpler explanations/examples than i found... so would it be split or map?

      strfry()
        I would use split...
        my @chars = split(//, $string);
        When you call split with an empty pattern "//", it automatically splits into individual characters and returns that list of characters.

        To print the results out as you have, then do this...

        print join("\n", split(//, $string)), "\n";
        The split() works as I stated above.
        The join takes each element of the list it is given in the second arguement and puts the first arguement after it, forming a string; Exception, then last item in the list does not get the first arguement appended to it -- that is why I added the last "\n".