Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

What do I do wrong? I want an array of arrays

by pcouderc (Monk)
on Sep 27, 2005 at 18:26 UTC ( [id://495488]=perlquestion: print w/replies, xml ) Need Help??

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

Sorry for my naïve question, but I should expect to get "yy" :

my @b;
my @c={ 22,44,55};
my @d={ "yy","tt"};
$b[0]=@c;
$b[1]=@d;
my @e= $b[1];
print "<$e[0]>\n";

Where am I wrong?

2005-09-29 Retitled by g0n, as per Monastery guidelines
Original title: 'What do i do wrong?'

  • Comment on What do I do wrong? I want an array of arrays

Replies are listed 'Best First'.
Re: What do I do wrong? I want an array of arrays
by Tanktalus (Canon) on Sep 27, 2005 at 18:42 UTC

    To answer the subject - you have a number of syntactical and semantic errors here.

    First, you probably want () rather than {} on the @c and @d initialisation lines:

    my @c = ( 22, 44, 55 ); my @d = ( 'yy', 'tt' ); # or my @d = qw(yy tt);
    This will create a list rather than a reference to a hash.

    Second, your assignments - an array, such as @b, can only hold scalars - not other arrays. That said, a reference to an array is a scalar. Thus, you probably want:

    $b[0] = \@c; # hold a reference to @c. $b[1] = \@d;
    However, once you do that, you need to dereference it when you want to copy it into @e.
    my @e = @{$b[1]};
    What you have right now creates arrays @c and @d, both of which contain a single element: a reference to a hash (again, a reference is a scalar of some type, thus can be in an array). Then you assign the "scalar" value of the array into $b[1] - the scalar value of an array is the length of the array (in this case, there's only one value in the array - the reference to the hash), and from there, you copy that 1 to @e as a single element.

    Hope that helps.

Re: What do I do wrong? I want an array of arrays
by friedo (Prior) on Sep 27, 2005 at 18:35 UTC
    { } is an anonymous hash reference constructor, which are you attempting to assign to the arrays @c and @d. @c and @d are arrays, which are you trying to assign to the scalar array elements $b[0] and $b[1].

    I don't know exactly what you're trying to do, but I would guess you want something like this.

    my @c = ( 22, 44, 55 ); my @d = ( 'yy', 'tt' ); my @b; @b[0,1] = ( $c[0], $d[0] ); my @e = ( $b[1] ); print "<$e[0]>\n";
      Mmm, yes, I should better explain what I am wanting to do. I want to get an array of arrays of variable size of strings. I have "blocks" of lines of text and i want to manipulate them. So I want to store 'yy' in the first line of my second block then get it back.
Re: What do I do wrong? I want an array of arrays
by sk (Curate) on Sep 27, 2005 at 18:45 UTC
    Where am I wrong?

    almost every line? :)

    Let's deal with this by running the program. first trun on strict and wranings warnings. See my first 2 lines in the code

    use strict; use warnings; my @b; my @c={ 22,44,55}; my @d={ "yy","tt"}; $b[0]=@c; # are you trying to create 2-D array? $b[1]=@d; my @e= $b[1]; # i think you want b as 2-D array print "<$e[0]>\n";

    when you run it as is you get this warning

    Odd number of elements in anonymous hash at nov line 7.<1> Once you change {} to  () the code works but it does not do what you want. See $b[0] is a scalar and an array when evaluated under the scalar context returns the lenght of the array so bascially  $b[0] = @c; will put 3 into $b[0] or the first element of b.

    If you want to do 2-D arrays you want to send them as references.

    The code below does that

    use strict; use warnings; my @b; my @c=( 22,44,55); # changed to () my @d=( "yy","tt"); # changed to () $b[0]=\@c; # \@ creates a reference to the array @c. $b[1]=\@d; my @e= @{$b[1]}; # $b[1] is an array ref so use @{} to get back +the list. print "<$e[0]>\n";

    output:

    <yy>

    hope this helps

Re: What do I do wrong? I want an array of arrays
by revdiablo (Prior) on Sep 27, 2005 at 18:48 UTC

    There are a few problems here. As friedo (Update: and others too, apparently) already pointed out, you're using anonymous hashref constructors where you appear to want a simple list grouping construct. Also, you're attempting to assign entire arrays as the value for $b[0] and $b[1], but array values can only be scalars (not arrays, or hashes).

    Put bluntly, it appears that you don't know a whole lot of Perl and are just trying things at random. Perhaps you should spend some more time learning the language, using a resource designed specifically for that? Maybe a stop over to http://learn.perl.org/ would do you well.

Thank you all
by pcouderc (Monk) on Sep 28, 2005 at 05:36 UTC
    I understand now that an array can contain only scalars and not other arrays.It may seem trivial for fine monks that you are, but it was not for me.
    In fact, I think that my need is for an array of arrays as explained in perllol.
    I always develop with "warnings" and "strict", and my syntax were correct here, even if it were a nonsense. The problem is that I do not use perl regularly enough, so I forget... Tnak you again.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-24 06:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found