Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: Array loops and assignments

by ww (Archbishop)
on Jul 07, 2013 at 19:25 UTC ( #1043003=note: print w/replies, xml ) Need Help??

in reply to Array loops and assignments

Great imagination; very little attempt to use actual Perl syntax. As a wise person observed, 'you can't just make s### up and expect the computer to understand.'

The use of strict and warnings would have revealed that there's far more wrong than the one syntax error you cite.
For example

  1. at line 1, you are trying to create an array... but you haven't declared it with "my ." (Did the "myNames" nomenclature confuse you? It's a waste of effort (at least in this case) to make the var name any longer than it needs to be for clarity. It might also have made it hard for you to recognize that declaration didn't happen.)
  2. at line 2, you've created (and declared; ++) the $id var, BUT it is badly misnamed as it will NOT hold an id number, but rather a name; one of the elements of the array.
    #!/usr/bin/perl use 5.016; use strict; use warnings; # -- WAG as in "It sure looks like madbee was doing wild +assed guessing." =head my @Names = ('Larry', 'Curly', 'Moe'); foreach my $id(0..$#Names) { say $id; # .... } output: C:\> 0 1 2 =cut my @Names = ('Larry', 'Curly', 'Moe'); my $id = 0; for my $name(@Names) { my $var = $name; my $varid = $var . $id; say $varid; ++$id; } #output 2 # Larry0 # Curly1 # Moe2
  3. As to breaking an array into multiple named variables, this is one (not the best, and limited but expansible) way:
    #!/usr/bin/perl use 5.016; use strict; use warnings; # my ($str, $list); my @Names = ('Larry', 'Curly', 'Moe'); for my $name(@Names) { $str .= ($name . ","); } (my $listitem1, my $listitem2, my $listitem3) = split /,/, $str; say "\$listitem1: $listitem1"; say "\$listitem2: $listitem2"; say "\$listitem3: $listitem3"; =head $listitem1: Larry $listitem2: Curly $listitem3: Moe =cut

    However, a little careful study of the docs on arrays will offer several more Perl-ish and more elegant approaches. Your results will vary, more of less proportionally to the effort you put into studying the docs and the standard "Learning Perl," "Intermediate Perl," ... texts.

If you didn't program your executable by toggling in binary, it wasn't really programming!

Replies are listed 'Best First'.
Re^2: Array loops and assignments
by madbee (Acolyte) on Jul 07, 2013 at 19:39 UTC

    @ww Thanks for your response. Yes, I did declare the variables and used strict,warnings etc. Didn't paste the entire code - just the array part. My bad!

    The requirement is that 8 elements of the array be stored in separate attributes. So instead of doing a single-assignment for each attribute, I was hoping to just put it in a loop and assign it to the corresponding variable. so, instead of: var1 = arr[0]; var2 = arr[1]; I was hoping to do: var[i] = arr[i] in a loop.

    Am sure there are more elegant and direct approaches to this. Thanks again. Will check the docs.

      It still comes back to the same question though. What can you do with $var1, $var2, $var3, $var4, etc. that you cannot do with $myNames[0], $myNames1, $myNames2...

      If you answer that, then I think you will get some more constructive solutions. What you're doing is assigning an existing numbered list of scalars, and trying to make a list of scalars which are numbered from it.

      If you really need to do a block assign, in a known order, then the construct for doing this is to put braces around your 'my' statement:

      my ( $var1, $var2, $var3, $var4 ) = @myNames;

      Will populate the vars as listed. But how do you use those var names later? You may well be jumping through a hoop you just don't need to.

      The real advantage of lists is that you actually very rarely need to reference individual elements, because you can do things like 'foreach'

      But you could do something like:

      my @var = @myNames;

      And then, you'll have $var[0] = "Curly" already.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2018-03-19 04:04 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (232 votes). Check out past polls.