in reply to Adding hashes to already existing array

Could you please show us, in pseudo-code if not an actual data structure in Data::Dumper style format, a snip of exactly how you want your structure to look like?

Replies are listed 'Best First'.
Re^2: Adding hashes to already existing array
by ExReg (Priest) on May 06, 2016 at 18:50 UTC
    $fc = 'abcdfoofrobnicatebardefforspambazghi'; $re2 = qr/(fo.)(.*?)(ba.)/; push @excerpts, $1 while $fc =~ /($re2)/g; print "0: $excerpts[0]; print "0: $excerpts[0]; for my $i ( 0 .. 1 ) { $excerpts[i] =~ /$re2/; $%{$excerpts[$i]}{fpart} = $1; $%{$excerpts[$i]}{bpart} = $3; } print "0{fpart}: $%{$excerpts[0]}{fpart}\n; print "0{bpart}: $%{$excerpts[0]}{bpart}\n; print "1{fpart}: $%{$excerpts[1]}{fpart}\n; print "1{bpart}: $%{$excerpts[1]}{bpart}\n; __END__ 0:foofrobnicatebar 1:forspambaz 0{fpart}: foo 0{bpart}: bar 1{fpart}: for 1{bpart}: baz

    Hope I typed all this correctly

    Edit: changed $2 to $3 in for loop. Running it on PC with perl yields above results with exception of 1{bpart} for some reason

    Addendum: I also realize that I could have started out with an array of hashes and put the original contents into $excerpts[$i]{contents} and then from it gotten $excerpts[$i]{bpart} $excerpts[$i]{fpart} with the normal AoH syntax, but this question is for an already existing array with stuff in $excerpts[$i].

      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

        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

        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.