Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Adding hashes to already existing array

by ExReg (Priest)
on May 06, 2016 at 18:50 UTC ( [id://1162375]=note: print w/replies, xml ) Need Help??


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

$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].

Replies are listed 'Best First'.
Re^3: Adding hashes to already existing array
by haukex (Archbishop) on May 06, 2016 at 19:35 UTC

    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

        #!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

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1162375]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found