arrays and hashes

Discussion in 'Perl Misc' started by Paul Lalli, Sep 15, 2006.

  1. Paul Lalli

    Paul Lalli Guest

    Oobi Van Doobi wrote:
    > Could someone please brief me a little on arrays and hashes.


    You should REALLY REALLY read a decent tutorial on Perl for these sorts
    of questions. I recommend starting in this case with
    http://perldoc.perl.org/perldata.html

    > For example:
    > What is the difference with () and {}?


    Parentheses are (usually) used to construct a list, which is used to
    populate an array or a hash:

    my @array = (1, 2, 3, 4);
    my %hash = (one => 1, two => 2, three => 3);

    Curly-braces have a variety of functions in Perl. They are used to
    access a specific value of a hash by enclosing a key:
    $hash{four} = 4;
    They are used to create an anonymous hash reference:
    my $hash_ref = { one => 1, two => 2, three => 3 } ;

    The are also used to create blocks and subroutines, as well as delimit
    a variable name inside a double-quoted string. There are other uses as
    well, but I don't think they apply to your question.

    > also:
    > do I need to enclose each element in an array with []?


    No. Brackets, like braces, have multiple functions. They are used to
    access a specific element from an array, by enclosing an index:

    $array[2] = 20;

    They are also used to create an anonymous array reference:
    my $array_ref = [ 1, 2, 3, 4];

    > I am trying to construct an array of hashes.


    You need to read a decent tutorial or two about this too.
    http://perldoc.perl.org/perlreftut.html
    http://perldoc.perl.org/perllol.html
    http://perldoc.perl.org/perldsc.html
    http://perldoc.perl.org/perlref.html

    > All is good but iterating over
    > the array there is only one element*S*...and iterating over the element
    > gives me the hash array*S*. ...
    > this is what i have
    > my @ary=
    > [
    > {
    > },
    > ...
    > {
    > },
    > ];


    This creates an array that contains exactly one element. That one
    element is a reference to an anonymous array. That anonymous array
    contains multiple references to anonymous hashes.

    Changing the outer [ ] to ( ) will be a good start, but you really need
    to read those Perldocs to understand multi-level structures in Perl.

    Paul Lalli
    Paul Lalli, Sep 15, 2006
    #1
    1. Advertising

  2. Paul Lalli

    David Squire Guest

    Oobi Van Doobi wrote:
    > Could someone please brief me a little on arrays and hashes.


    Perl is your friend. It comes with documentation included so that people
    don't have to type this stuff again and again on Usenet. See:

    perldoc perldata
    perldoc perldsc

    If you would prefer to read these online, they are available at
    http://www.perl.com/pub/q/documentation

    > For example:
    > What is the difference with ()


    allows you to construct a list

    and {}?

    allows you to construct a reference to an anonymous hash.

    > also:
    > do I need to enclose each element in an array with []?


    No.

    > I am trying to construct an array of hashes.


    There is a whole section on this topic in perldoc perldsc

    Here is an example:

    ----

    #!/usr/bin/perl

    use strict;
    use warnings;

    my @array_of_hashes = ( # start defining a list, the contents of the array
    { # start the first element of the list, which is an anonymous hashref
    'cat' => 'mammal',
    'frog' => 'amphibian',
    'snake' => 'reptile',
    },
    { # start another list element
    'cup' => 'crockery',
    'knife' => 'cutlery',
    },
    );

    foreach my $array_element (@array_of_hashes) {
    print "###################################\n";
    foreach my $key ( keys %{$array_element} ) { # notice we dereference
    the element
    print "$key: $$array_element{$key}\n";
    }
    }

    ----

    Output:

    ###################################
    cat: mammal
    snake: reptile
    frog: amphibian
    ###################################
    knife: cutlery
    cup: crockery


    DS
    David Squire, Sep 15, 2006
    #2
    1. Advertising

  3. Could someone please brief me a little on arrays and hashes.
    For example:
    What is the difference with () and {}?
    also:
    do I need to enclose each element in an array with []?

    I am trying to construct an array of hashes. All is good but iterating over
    the array there is only one element*S*...and iterating over the element
    gives me the hash array*S*. ...
    this is what i have
    my @ary=
    [
    {
    },
    ...
    {
    },
    ];
    Thank you,
    any advice much appreciated
    Oobi Van Doobi, Sep 15, 2006
    #3
  4. Paul Lalli wrote:

    > Oobi Van Doobi wrote:
    >> Could someone please brief me a little on arrays and hashes.

    >
    > You should REALLY REALLY read a decent tutorial on Perl for these sorts
    > of questions. I recommend starting in this case with
    > http://perldoc.perl.org/perldata.html
    >
    >> For example:
    >> What is the difference with () and {}?

    >
    > Parentheses are (usually) used to construct a list, which is used to
    > populate an array or a hash:
    >
    > my @array = (1, 2, 3, 4);
    > my %hash = (one => 1, two => 2, three => 3);
    >
    > Curly-braces have a variety of functions in Perl. They are used to
    > access a specific value of a hash by enclosing a key:
    > $hash{four} = 4;
    > They are used to create an anonymous hash reference:
    > my $hash_ref = { one => 1, two => 2, three => 3 } ;
    >
    > The are also used to create blocks and subroutines, as well as delimit
    > a variable name inside a double-quoted string. There are other uses as
    > well, but I don't think they apply to your question.
    >
    >> also:
    >> do I need to enclose each element in an array with []?

    >
    > No. Brackets, like braces, have multiple functions. They are used to
    > access a specific element from an array, by enclosing an index:
    >
    > $array[2] = 20;
    >
    > They are also used to create an anonymous array reference:
    > my $array_ref = [ 1, 2, 3, 4];
    >
    >> I am trying to construct an array of hashes.

    >
    > You need to read a decent tutorial or two about this too.
    > http://perldoc.perl.org/perlreftut.html
    > http://perldoc.perl.org/perllol.html
    > http://perldoc.perl.org/perldsc.html
    > http://perldoc.perl.org/perlref.html
    >
    >> All is good but iterating over
    >> the array there is only one element*S*...and iterating over the element
    >> gives me the hash array*S*. ...
    >> this is what i have
    >> my @ary=
    >> [
    >> {
    >> },
    >> ...
    >> {
    >> },
    >> ];

    >
    > This creates an array that contains exactly one element. That one
    > element is a reference to an anonymous array. That anonymous array
    > contains multiple references to anonymous hashes.
    >
    > Changing the outer [ ] to ( ) will be a good start, but you really need
    > to read those Perldocs to understand multi-level structures in Perl.
    >
    > Paul Lalli

    oh, !!thank's!!
    Oobi Van Doobi, Sep 15, 2006
    #4
  5. Paul Lalli

    Paul Lalli Guest

    Abigail wrote:
    > David Squire () wrote on MMMMDCCLXIII
    > September MCMXCIII in <URL:news:eeeauv$jr1$>:
    > ;;
    > ;; > What is the difference with ()
    > ;;
    > ;; allows you to construct a list
    >
    > Eh, not really. Context makes lists - the only place where () is significant
    > in creating a list is on the left hand side of the x.


    Hrm. How does that jive with things such as:

    my $x = @foo;
    vs
    my ($x) = @foo;

    The way I understand it, in the first, @foo is evaluated in scalar
    context, because we're assigning to a scalar value, and so $x gets the
    size of @foo. In the second, @foo is evaluated in list context,
    because we're assigning to a list, and so $x gets the first value of
    @foo. So didn't the parentheses change "$x" from "a scalar value" to
    "a list containing a single scalar value"?

    Paul Lalli
    Paul Lalli, Sep 15, 2006
    #5
  6. Paul Lalli

    -berlin.de Guest

    Paul Lalli <> wrote in comp.lang.perl.misc:
    > Abigail wrote:
    > > David Squire () wrote on MMMMDCCLXIII
    > > September MCMXCIII in <URL:news:eeeauv$jr1$>:
    > > ;;
    > > ;; > What is the difference with ()
    > > ;;
    > > ;; allows you to construct a list
    > >
    > > Eh, not really. Context makes lists - the only place where () is significant
    > > in creating a list is on the left hand side of the x.

    >
    > Hrm. How does that jive with things such as:
    >
    > my $x = @foo;
    > vs
    > my ($x) = @foo;
    >
    > The way I understand it, in the first, @foo is evaluated in scalar
    > context, because we're assigning to a scalar value, and so $x gets the
    > size of @foo. In the second, @foo is evaluated in list context,
    > because we're assigning to a list, and so $x gets the first value of
    > @foo. So didn't the parentheses change "$x" from "a scalar value" to
    > "a list containing a single scalar value"?


    That is what Abigail said: () creates list context when used on the left
    side of an assignment.

    Anno
    -berlin.de, Sep 18, 2006
    #6
  7. Paul Lalli

    Paul Lalli Guest

    -berlin.de wrote:
    > Paul Lalli <> wrote in comp.lang.perl.misc:
    > > Abigail wrote:
    > > > Eh, not really. Context makes lists - the only place where () is significant
    > > > in creating a list is on the left hand side of the x.


    > That is what Abigail said: () creates list context when used on the left
    > side of an assignment.



    Okay.... I guess I didn't interpret "left hand side of the x" as "left
    side of an assignmen".

    Paul Lalli
    Paul Lalli, Sep 18, 2006
    #7
  8. Paul Lalli

    David Squire Guest

    -berlin.de wrote:
    > Paul Lalli <> wrote in comp.lang.perl.misc:
    >> Abigail wrote:
    >>> David Squire () wrote on MMMMDCCLXIII
    >>> September MCMXCIII in <URL:news:eeeauv$jr1$>:
    >>> ;;
    >>> ;; > What is the difference with ()
    >>> ;;
    >>> ;; allows you to construct a list
    >>>
    >>> Eh, not really. Context makes lists - the only place where () is significant
    >>> in creating a list is on the left hand side of the x.


    I am happy to be corrected on this, but the usage I was referring to, in
    the context of the OP's original post, is something like this:

    my @new_array = (1, 2, 3);

    as opposed to, say,

    my $new_array_ref = [1, 2, 3];

    Is my understanding of the usage of (...) in the first instance flawed?



    DS
    David Squire, Sep 18, 2006
    #8
  9. Paul Lalli

    Ben Morrow Guest

    Quoth David Squire <>:
    > -berlin.de wrote:
    > > Paul Lalli <> wrote in comp.lang.perl.misc:
    > >> Abigail wrote:
    > >>> David Squire () wrote on MMMMDCCLXIII
    > >>> September MCMXCIII in <URL:news:eeeauv$jr1$>:
    > >>> ;;
    > >>> ;; > What is the difference with ()
    > >>> ;;
    > >>> ;; allows you to construct a list
    > >>>
    > >>> Eh, not really. Context makes lists - the only place where () is significant
    > >>> in creating a list is on the left hand side of the x.

    >
    > I am happy to be corrected on this, but the usage I was referring to, in
    > the context of the OP's original post, is something like this:
    >
    > my @new_array = (1, 2, 3);
    >
    > as opposed to, say,
    >
    > my $new_array_ref = [1, 2, 3];
    >
    > Is my understanding of the usage of (...) in the first instance flawed?


    It's not flawed, it's just that it's not the () that create the list.
    The comma creates the list; the () simply prevents it from being parsed
    as

    (my @new_array = 1), 2, 3;

    as = binds tighter than , (in that direction).

    Ben

    --
    'Deserve [death]? I daresay he did. Many live that deserve death. And some die
    that deserve life. Can you give it to them? Then do not be too eager to deal
    out death in judgement. For even the very wise cannot see all ends.'
    Ben Morrow, Sep 18, 2006
    #9
  10. Paul Lalli

    -berlin.de Guest

    David Squire <> wrote in comp.lang.perl.misc:
    > -berlin.de wrote:
    > > Paul Lalli <> wrote in comp.lang.perl.misc:
    > >> Abigail wrote:
    > >>> David Squire () wrote on MMMMDCCLXIII
    > >>> September MCMXCIII in <URL:news:eeeauv$jr1$>:
    > >>> ;;
    > >>> ;; > What is the difference with ()
    > >>> ;;
    > >>> ;; allows you to construct a list
    > >>>
    > >>> Eh, not really. Context makes lists - the only place where () is significant
    > >>> in creating a list is on the left hand side of the x.

    >
    > I am happy to be corrected on this, but the usage I was referring to, in
    > the context of the OP's original post, is something like this:
    >
    > my @new_array = (1, 2, 3);
    >
    > as opposed to, say,
    >
    > my $new_array_ref = [1, 2, 3];
    >
    > Is my understanding of the usage of (...) in the first instance flawed?


    It is correct, it is even necessary, but it has nothing to do with list
    context.

    "=" binds rather tightly in Perl, so leaving out the () is interpreted
    as

    ( @new_array = 1), 2, 3;

    (which is worth a warning).

    The parens around ( 1, 2, 3) serve to change the precedence, just like
    in "($x + $y)*$z".

    Anno
    -berlin.de, Sep 18, 2006
    #10
  11. Paul Lalli

    Ch Lamprecht Guest

    David Squire wrote:
    > -berlin.de wrote:
    >
    >> Paul Lalli <> wrote in comp.lang.perl.misc:
    >>
    >>> Abigail wrote:
    >>>
    >>>> David Squire () wrote on MMMMDCCLXIII
    >>>> September MCMXCIII in <URL:news:eeeauv$jr1$>:
    >>>> ;;
    >>>> ;; > What is the difference with ()
    >>>> ;;
    >>>> ;; allows you to construct a list
    >>>>
    >>>> Eh, not really. Context makes lists - the only place where () is
    >>>> significant
    >>>> in creating a list is on the left hand side of the x.

    >
    >
    > I am happy to be corrected on this, but the usage I was referring to, in
    > the context of the OP's original post, is something like this:
    >
    > my @new_array = (1, 2, 3);
    >
    > as opposed to, say,
    >
    > my $new_array_ref = [1, 2, 3];
    >
    > Is my understanding of the usage of (...) in the first instance flawed?


    (...) are for precedence in this case as demonstrated by the following piece of
    code:

    use strict;
    use warnings;

    my @array;
    my $counter = 0;

    @array = (getval(),getval(),getval());
    @array = getval(),getval(),getval() ;

    sub getval{
    print "array is now @array\n";
    print "getval returns $counter\n";
    return $counter++;
    }
    print "array is now @array\n";



    Christoph
    --

    perl -e "print scalar reverse q//"
    Ch Lamprecht, Sep 18, 2006
    #11
  12. Paul Lalli

    David Squire Guest

    -berlin.de wrote:
    > David Squire <> wrote in comp.lang.perl.misc:
    >> -berlin.de wrote:
    >>> Paul Lalli <> wrote in comp.lang.perl.misc:
    >>>> Abigail wrote:
    >>>>> David Squire () wrote on MMMMDCCLXIII
    >>>>> September MCMXCIII in <URL:news:eeeauv$jr1$>:
    >>>>> ;;
    >>>>> ;; > What is the difference with ()
    >>>>> ;;
    >>>>> ;; allows you to construct a list
    >>>>>
    >>>>> Eh, not really. Context makes lists - the only place where () is significant
    >>>>> in creating a list is on the left hand side of the x.

    >> I am happy to be corrected on this, but the usage I was referring to, in
    >> the context of the OP's original post, is something like this:
    >>
    >> my @new_array = (1, 2, 3);
    >>
    >> as opposed to, say,
    >>
    >> my $new_array_ref = [1, 2, 3];
    >>
    >> Is my understanding of the usage of (...) in the first instance flawed?

    >
    > It is correct, it is even necessary, but it has nothing to do with list
    > context.
    >
    > "=" binds rather tightly in Perl, so leaving out the () is interpreted
    > as
    >
    > ( @new_array = 1), 2, 3;
    >
    > (which is worth a warning).
    >
    > The parens around ( 1, 2, 3) serve to change the precedence, just like
    > in "($x + $y)*$z".


    Ah. Thanks very much. I guess it is a happy coincidence that the
    parentheses for precedence here make the RHS look like what you would
    want if you were inducing list context on the LHS.


    DS
    David Squire, Sep 18, 2006
    #12
  13. Paul Lalli

    -berlin.de Guest

    David Squire <> wrote in comp.lang.perl.misc:
    > -berlin.de wrote:
    > > David Squire <> wrote in comp.lang.perl.misc:
    > >> -berlin.de wrote:


    [...]

    > >> my @new_array = (1, 2, 3);
    > >>
    > >> as opposed to, say,
    > >>
    > >> my $new_array_ref = [1, 2, 3];
    > >>
    > >> Is my understanding of the usage of (...) in the first instance flawed?

    > >
    > > It is correct, it is even necessary, but it has nothing to do with list
    > > context.
    > >
    > > "=" binds rather tightly in Perl, so leaving out the () is interpreted
    > > as
    > >
    > > ( @new_array = 1), 2, 3;
    > >
    > > (which is worth a warning).
    > >
    > > The parens around ( 1, 2, 3) serve to change the precedence, just like
    > > in "($x + $y)*$z".

    >
    > Ah. Thanks very much. I guess it is a happy coincidence that the
    > parentheses for precedence here make the RHS look like what you would
    > want if you were inducing list context on the LHS.


    It is one of the things that make Perl look more consistent than it
    is.

    Anno
    -berlin.de, Sep 18, 2006
    #13
  14. Paul Lalli

    Dr.Ruud Guest

    David Squire schreef:

    > my @new_array = (1, 2, 3);
    >
    > as opposed to, say,
    >
    > my $new_array_ref = [1, 2, 3];
    >
    > Is my understanding of the usage of (...) in the first instance
    > flawed?


    $ perl -le '@_=1,2,3; print "@_"'
    1


    $ perl -le '@_=(1,2,3); print "@_"'
    1 2 3


    $ perl -le '@_=@{[1,2,3]}; print "@_"'
    1 2 3


    $ perl -le '@_=do{1,2,3}; print "@_"'
    1 2 3


    $ perl -le '$_=(@_=4,5,6); print "@_\n$_"'
    4
    6


    $ perl -le '$_=(@_=(4,5,6)); print "@_\n$_"'
    4 5 6
    3

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Sep 18, 2006
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Holness

    Hashes of Hashes via subs

    Ben Holness, Oct 5, 2003, in forum: Perl
    Replies:
    8
    Views:
    561
    Ben Holness
    Oct 8, 2003
  2. Steven Arnold

    using hashes as keys in hashes

    Steven Arnold, Nov 23, 2005, in forum: Ruby
    Replies:
    3
    Views:
    160
    Mauricio Fernández
    Nov 23, 2005
  3. kazaam
    Replies:
    12
    Views:
    271
    Matthias Wächter
    Sep 13, 2007
  4. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    211
  5. Replies:
    3
    Views:
    206
Loading...

Share This Page