-4

I have a perl script where we are converting Hash into array-

my %info = (
          'name'    => $1,
          'ip'      => $2,
          'parent'  => '',
          'isLocal' => 0,
          'clip'    => 0,
          );
my @siteinfos = ();
push( @siteinfos, \%info );

Now I am able to accesss the array by-

foreach my $site(@siteinfos) {
if ( $site->{'name'} eq  $sitename)  {
 .....
}

First I am not sure how this conversion actually works.Secondly,Now I want to add more elements to this array in (key,pair) format.How can I do so?

4
  • 1
    Possible duplicate of Perl: array and hash Commented Jan 17, 2017 at 14:51
  • ... and besides that one (see this answer), try this question. Commented Jan 17, 2017 at 14:52
  • 1
    Where did sinfos var come from? Commented Jan 17, 2017 at 14:56
  • edited the code Commented Jan 17, 2017 at 14:58

2 Answers 2

7

First, there is no conversion.

my @siteinfos = ();

defines an array @siteinfos and sets to the empty list. Of course, you could have achieved the same result by using just

my @siteinfos;

Next, the statement

push( @siteinfos, \%info );

pushes a reference to the hash %info to @siteinfos as the sole element of that array.

Next, the loop

foreach my $sinfo (@siteinfos) { # assuming original is a typo

ends up aliasing $sinfo to that sole hash reference in @siteinfos. In the body of the loop, you access an element of the original hash via the reference you pushed to @siteinfos.

Now, things would have been different if you had done:

push @siteinfos, %info;

That would have set @siteinfos to a list like the following (the order is non-deterministic):

$VAR1 = [
          'ip',
          undef,
          'clip',
          0,
          'isLocal',
          0,
          'name',
          undef,
          'parent',
          ''
        ];

The undef are there because the match variables were empty when I ran the script. Now, if you want to push another key-value pair to this array, that's trivial:

 push @siteinfos, $key, $value;

But of course, by assigning the hash to an array, you are losing the ability to simply look up values by key. In addition, you can keep pushing duplicate keys to an array, but if you assign it back to another hash:

my %otherinfo = @siteinfos;

then only the last version of an even-indexed element (key) and its corresponding odd-indexed element (value) will survive.

Sign up to request clarification or add additional context in comments.

3 Comments

We not only have the same kind of answer, we even used the same way to format it. :D
@simbabque If you want to combine the second part of my answer regarding pushing more key-value pairs to the array, I'll delete mine.
Nonono, you only need 5k more rep for your t-shirt. ;) We can all have a Stack Overflow t-shirt party at YAPC::EU in Amsterdam this summer.
5

You are not converting anything. You are making a new array named @siteinfos.

my @siteinfos = ();

Then you add one entry, which is a reference to the hash %info.

push( @siteinfos, \%info );

The array now holds a reference to %info. That's $siteinfo[0].

In your loop you iterate all the elements in @siteinfos. The hashref now goes into $sinfo.

If you want to add more keys and values to that hashref, just put them in.

foreach my $sinfo (@siteinfos) {
    if ( $sinfo->{'name'} eq  $sitename)  {
        $sinfo->{foo} = "bar"; # as simple as this
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.