Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: array or scalar, that's the question

by Parham (Friar)
on Jan 01, 2002 at 21:38 UTC ( #135525=note: print w/replies, xml ) Need Help??

in reply to array or scalar, that's the question

well it seems like no matter what you do... you're going to end up with an array cuz that's what your returning from &someonesFunc() (you don't need the ampersand on that btw). I think the solution you already have is a very good one and i can't see why you'd want to even change it.

In both cases though, you're always going to have an array, either with one or more elements.
  • Comment on Re: array or scalar, that's the question

Replies are listed 'Best First'.
Re: Re: array or scalar, that's the question
by hotshot (Prior) on Jan 01, 2002 at 21:47 UTC
    to clear things up a little. the function I use (someonesFunc() in the last post) is something from the format:
    if ($x) { return $foo; # returns scalar }else{ return @bar; # returns array }
    I just thought there's a better way then mine to know what it returned, but if you suggest that what I did is good then what that's good enough for me.

      Differentiating between a "scalar" and an "array" is meaningless here. When you say "return @bar" Perl flattens @bar into a list.
      $foo = 3; @bar = (1, 2, 3); return $foo; # (3) return @bar; # (1, 2, 3)

      In a scalar context, both of these versions will return 3 (the last element in the list returned). In a list context, you get 3 or 1, 2, 3. Just returning @bar, though, is sufficient for both cases, because it's your calling code that would want to act on the results. Make sense?

      If you're wanting to return a real solid array (so that you can return @foo, @bar without flattening it into one big list), you need to return references to each array.

      return (\@foo, \@bar); ... my ($fooref, $barref) = your_function();
        When you say "return @bar" Perl flattens @bar into a list.
        That's what happens in _list_ context. If the sub is called in scalar context, @bar will also be in scalar context and thus return the number of elements in @bar.

        sub foo { my @foo = qw(a b c d); return @foo; } sub bar { return qw(15 4 3 12); } print foo(), "\n"; # list context => abcd\n print scalar foo(), "\n"; # scalar context => 4\n print bar(), "\n"; # list context => 154312\n print scalar bar(), "\n"; # scalar context => 12\n

        Explanation: a list returns its last element in scalar context, an array returns the number of elements in scalar context.

        2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://135525]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2017-06-25 05:53 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (564 votes). Check out past polls.