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

Hi

What I’m looking to do is to assign 3 elements of an array to 3 different scalar values.

Eg:- $stuff[0] Value should be assigned to new scalar
$stuff1[1] Value should be assigned to new scalar
$STUFF[2] Value should be assigned to new scalar

Any ideas on the best way to do this.

Hope this is clear, :)

Replies are listed 'Best First'.
Re: Assigning elements of an array to scalars
by FunkyMonk (Bishop) on Oct 10, 2007 at 16:18 UTC
    Assuming you mean you want to assign the first three elements of @stuff to three new scalars:
    my ( $new0, $new1, $new2 ) = @stuff;

Re: Assigning elements of an array to scalars
by Fletch (Bishop) on Oct 10, 2007 at 16:19 UTC

    Formatting issues aside, what you're asking doesn't really make much sense. The usual admonition is to go the other way instead; e.g. if you've got $stuff1, $stuff2, etc. it's a good indication that you really should have them all in an array @stuff instead of having separate variables. See MJD's Why it's stupid to `use a variable as a variable name' for more discussion (although that's more on hashes rather than arrays the concept's the same).

Re: Assigning elements of an array to scalars
by toolic (Bishop) on Oct 10, 2007 at 16:16 UTC
    Here is one way to do what I think you want to do:
    use warnings; use strict; my @stuff = (5..7); my ($stuff1, $stuff2, $stuff3) = @stuff; print "stuff1=$stuff1\n"; print "stuff2=$stuff2\n"; print "stuff3=$stuff3\n"; __END__ Prints out: stuff1=5 stuff2=6 stuff3=7
Re: Assigning elements of an array to scalars
by naikonta (Curate) on Oct 10, 2007 at 18:11 UTC
    An array is a form of list. To assign an array value into scalars then you use a list of scalars, like my($stuff1, $stuff2, $stuff3) = @array;. If you want to assign only the first few elements, just use a number of scalars you need, so ignoring the rest. This is better than you assign all of them but never use one or more of scalars.
    my($stuff1, $stuff2) = @array; # eventough it has more than two elemen +ts my($stuff1, $stuff2) = @array; # if you never use $stuff2 for the rest of your program, then you're w +asting memory used by $stuff2.
    Instead, you can write my($stuff1) = @array;, or, my $stuff1 = $array[0];, or even my $stuff1 = shift @array; if you don't have to care to preserve @array. However, if you will never be interested in some elements in the middle, you can use undef, such as
    # skipping the second and third elements my($stuff1, undef, undef, $stuff4) = @array;
    If @array contains many elements, then it's probably wiser to use a hash instead of a bunch of scalar variables.
    # using scalars my($name, $age, $birthday, $address, $zipcode, $city, $country) = @arr +ay; # using hash my @keys = qw(name age birthday address zipcode city country); my %user; @user{ @keys } = @array; # so, instead of print "name = $name\n"; # you can write print "name = $user{name}\n";

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

      my($stuff1, undef, undef, $stuff4) = @array;

      can also be done as follows:

      my($stuff1, $stuff4) = @array[0,3];
Re: Assigning elements of an array to scalars
by mwah (Hermit) on Oct 10, 2007 at 16:17 UTC
    You didn't specify your question enough to
    give you an answer. Whats meant by all
    that $stuff[..] and scalar?

    After reading naikontas reply, I confess that I gave
    the wrong advice here:

    Please insert your code-containing text parts
    into a "pre"-block, like:
    <pre>
       insert problem description (including some code) here
    </pre>

    or if its really code, p

    The second part: "Put it in a"
    <code>

       insert your code here

    </code>

    "code block" would be the only reasonable thing
    to do (sorry about that).

    Regards

    mwa
      Per guidelines on Perl Monks Approved HTML tags, <pre> tags are discouraged.
      Although these tags are allowed, use of them is discouraged. In particular, it is usually much better to use <p> and not <br /> and please use <code> instead of <pre> unless the enclosed lines are pretty short.

      Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!