in reply to Re^2: Adding hashes to already existing array
in thread Adding hashes to already existing array

Hi ExReg,

I'd recommend you take a look at perldsc for a cookbook of different data structures. You should also always Use strict and warnings, especially when working with complex data structures - and your code contains a typo that prevents it from working properly and that use strict; would have caught! Also, please post code that compiles, you're missing several closing quotes.

The syntax "$%{$excerpts[$i]}{fpart}" is probably not doing what you want - it's populating a hash "%%"!

Here's one way to do what you want. Note that $excerpts[i]{fpart} = ... would not work, since at that point $excerpts[i] is a string, not a hash ref, that's why I replace that element of @excerpts with a new hashref.

use warnings; use strict; use Data::Dumper; my $fc = 'abcdfoofrobnicatebardefforspambazghi'; my $re2 = qr/(fo.)(.*?)(ba.)/; my @excerpts; push @excerpts, $1 while $fc =~ /($re2)/g; for my $i ( 0 .. $#excerpts ) { $excerpts[$i] =~ /$re2/; $excerpts[$i] = { fpart=>$1, bpart=>$3 }; } print Dumper(\@excerpts); __END__ $VAR1 = [ { 'bpart' => 'bar', 'fpart' => 'foo' }, { 'fpart' => 'for', 'bpart' => 'baz' } ];

Hope this helps,
-- Hauke D

Replies are listed 'Best First'.
Re^4: Adding hashes to already existing array
by ExReg (Priest) on May 06, 2016 at 20:22 UTC

    I apologize. I do use strict and warnings. That and any typos I have are because I do not have perl on this system and have to look at the other system where I test it to type it here.

    I would agree that the syntax I have is not what I want. As I mentioned in my reply to stevieb, a normal AoH syntax would have been better.

    If I understand what you have, you get the strings into $excerpts[$i], then replace those strings with hashes in the

    $excerpts[$i] = { fpart=>$1, bpart=>$3 };
    step. This unfortunately gets rid of the original string in each array element. I could get around this by doing
    $excerpts[$i] = { fpart=>$1, bpart=>$3, content=>$value };
    if I first get the content into $value before overwriting it.

    Thanks

      #!perl use strict; use Data::Dump 'pp'; my @excerpts = (); my $fc = 'abcdfoofrobnicatebardefforspambazghi'; my $re2 = qr/(fo.)(.*?)(ba.)/; while ($fc =~ /$re2/g){ push @excerpts, { fpart => $1, bpart => $3, content => $1.$2.$3 } ; } pp @excerpts;
      poj

      Hi ExReg,

      if I first get the content into $value before overwriting it

      $excerpts[$i] = { fpart=>$1, bpart=>$3, content=>$excerpts[$i] }; will do what you want.

      Hope this helps,
      -- Hauke D

        Exactly what I wanted

Re^4: Adding hashes to already existing array
by ExReg (Priest) on May 11, 2016 at 14:11 UTC
    The syntax "$%{$excerpts[$i]}{fpart}" is probably not doing what you want - it's populating a hash "%%"!

    Thank you for the bit about it actually being the hash %%. I thought I knew references well enough and now I guess I didn't. I have been re-reading the books and docs on references for the last few days and hope to never again utter such execrable desecrations.

      Hi ExReg,

      You may want to have a look at perlcritic, which can be helpful in catching some potential issues in addition to strict and warnings. For example, in this case it would have issued a warning 'Magic variable "$%" should be assigned as "local"' in regards to the %% hash (its analysis is not perfect but in this case at least it gives a hint), and a warning 'Capture variable used outside conditional', which stevieb already correctly pointed out. If you run perlcritic --verbose 10 ..., you will get a brief explanation of the issue as well. perlreftut and perlref are also good sources of information on references.

      Hope this helps,
      -- Hauke D