Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

(jeffa) Re: Arrays

by jeffa (Bishop)
on May 05, 2002 at 22:17 UTC ( [id://164180]=note: print w/replies, xml ) Need Help??


in reply to How do I reset an array?

You can 'reset' an array like so:
@arr = ();
But you don't need to do that to get your code to work:
use strict; # ALWAYS!!! my @array = &getarray(); my $blah; foreach my $val (@array) { $blah .= ",$val"; } print "$blah\n"; sub getarray { return ('a'..'d'); }
Also, don't forget about join which will replace the for loop and concatention for you (and do a much better job anyway):
print join(',', getarray()), "\n";

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re: (jeffa) Re: Arrays
by dsheroh (Monsignor) on May 06, 2002 at 19:45 UTC
    my @array = &getarray();

    I tried it out and see that this works, but it doesn't seem to me that it should, since you appear to be storing a reference to the function in the array rather than the list of values returned by the function. Is this just a technique for deferring execution of getarray until the foreach my $val (@array) or is something else going on here?

      No deferrence going on here - &get_array() simple returns an array and not a reference (sub or array). Maybe this code will shed some light:
      use strict; use Data::Dumper; print Dumper get_array(); print Dumper get_array_ref(); print Dumper scalar get_something(); print Dumper get_something(); sub get_array { return ('a'..'d'); } sub get_array_ref { return [('a'..'d')]; } sub get_something { return wantarray ? ('a'..'d') : 'scalar'; }
      Also check out the difference between a list and an array

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
        thanks, i appreciate all your help :)
        Im going to use all that info right now
        ^jasper <jasper@wintermarket.org>
        I get (no pun intended) how the original get_array works. What I'm unclear on is why you called it with &get_array() instead of just get_array(). I'm used to seeing \& used when setting up references to subs, but this is the first time I've encountered &subname() without the backslash.
      That line just calls the sub and puts whatever is returned into @array. If you wanted to store a reference to a sub you would do this:
      my $subref = \&getarray;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://164180]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-26 08:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found