Loading ...
Sorry, an error occurred while loading the content.
 

RE: [PBML] Re: I need help with hash of array (and references). U rgent please.

Expand Messages
  • GARCIA Lionel
    Hum... In fact, I found the answer. Here is the right code: while ( ) { my ($name, @datas)=split /:/; $reference{$name}=[@datas]; } The brackets are
    Message 1 of 1 , Apr 22, 2002
      Hum... In fact, I found the answer. Here is the right code:

      while (<REFERENCE>) {
      my ($name, @datas)=split /:/;
      $reference{$name}=[@datas];
      }

      The brackets are used to create an reference to an anonymous array contening
      @datas. I think that may help some beginners, like me ;-)

      Seems I'll have to think before I post to this mailing list....

      -----Message d'origine-----
      De: daymobrew [mailto:daymobrew@...]
      Date: lundi 22 avril 2002 14:12
      À: perl-beginner@yahoogroups.com
      Objet: [PBML] Re: I need help with hash of array (and references).
      Urgent please.


      --- In perl-beginner@y..., GARCIA Lionel <lionel.garcia@a...> wrote:
      > Hi,
      > I'm trying to read a file composed by many fields separed by ':',
      and then
      > store them in a hash which key is the first field, and value is an
      array
      > composed by the rest of the line. I'm using this code:
      >
      > while (<REFERENCE>) {
      > my ($name, @datas)=split /:/;
      > $reference{$name}=\@datas;
      > }
      >
      > But of course, and that's why I need help, all my elements in hash
      refer to
      > the last item read. So how could I store a reference to each entry ?
      >
      > Cordialement,
      >
      > Lionel GARCIA
      > Sogeti France - Support stations Unix
      > * : lionel.garcia@a...

      Using the code you provide, you can print out the contents of the
      hash data with:
      foreach ( keys %reference )
      {
      print "$_ = ", join( ',', @{ $reference{ $_ } } ), "\n";
      }

      You are creating a hash of arrays. See perldsc for more information.




      Unsubscribing info is here:
      http://help.yahoo.com/help/us/groups/groups-32.html

      Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    Your message has been successfully submitted and would be delivered to recipients shortly.