in reply to Re: Find a Position and Insert Text
in thread Find a Position and Insert Text

Thank you for your reply and code.

This is just way too advanced for me at present.

I am still learning Perl etc.

John

Replies are listed 'Best First'.
Re^3: Find a Position and Insert Text
by Your Mother (Archbishop) on Mar 16, 2016 at 20:44 UTC

    I totally understand. I remember the same boat quite well. That said, the solution, or at least its style, I gave is the only sane way to approach your problem and by far the easiest. Regular expressions can indeed move mountains but it's much harder than you're thinking and for your problem I doubt you (or anyone for that matter) would ever arrive at anything that worked except in the most trivial and inflexible case.

    My code was idiomatic but it is easy and straightforward once you can follow it. It's also robust whereas a regex solution will never be. I'll adapt the code a bit to make it more clear and walk you through it. <readmore/> away…

    Have fun, HTH, TTFN, etc. :P

    Further Reading

    Update: </c></c> fixed.

      In the original data, age1 occurs two times (with potentially two different values), while in your approach, both entries will share the same value. I don't know if that is important for the OP though.

        Good eye++. It is the same issue as Perl hashes though. Duplicate Object keys in JS are a mistake and will be clobbered.

        <script type="text/javascript"> var p = { "age1":"one", "age1":"two" }; for ( var key in p ) { if ( p.hasOwnProperty(key) ) { document.write(key + " -> " + p[key] + "<br/>"); } } </script> age1 -> two

      Thank you for your time and effort with your reply

      I really appreciate it.

      Below is just your code with some comments/questions etc.

      Would appreciate your comments at some stage.

      I am really still learning Perl.

      use warnings; use strict; use JSON; use Data::Dumper; my $original = '{"OWNER":"KeyProjects","age1":null,"ht1":null}';

      #Literal entry.

      my $desired  = '{"OWNER":"KeyProjects","scheduled":{"age1":null,"ht1":null}}';

      #Literal entry.

      my $data = decode_json($original); print Dumper $original; print Dumper $desired; print Dumper $data; for my $key ( keys % {$data} )

      #Converts $data into a hash and cycles through it.

      { print "Found a key: $key", $/; if ( $key =~ /\A (?: age\d+ | ht\d+ ) \z/x ) { print " -> It matches our regular expression\n"; print " -> Deleting it from data structure\n"; my $value = delete $data->{$key};

      #Does this delete the pair, the value, or what? See below.

      print Dumper $key;

      #Looks like nothing is deleted.

      print Dumper $value;

      #Looks like nothing is deleted.

      print " -> Putting it back under sub-structure with key 'sche +duled'\n"; $data->{scheduled}{$key} = $value;

      #I should understand this step but I don't really know what each aspect of this line is doing.

      print Dumper $key; print Dumper $value; print Dumper $data; } else { print " -> It's fine where it is, no action taken\n"; print Dumper $key; print Dumper $data; } } print encode_json( $data ), $/; use Test::More; my $desired_data = decode_json( $desired ); is_deeply( $desired_data, $data, "Data structures match" ); done_testing(1);

        I’m glad it’s got you thinking. Here’s a little more to play with. The only part it answers directly is the delete function. I think you will start to understand more the more you play around with the code. Part of what makes Perl so easy/fun is how easy it is to test ideas, adjust and run. Remember, you have great built-in documentation. Just run perldoc -f delete for functions for example. Or perldoc perltoc or perldoc Data::Dumper, etc… :P

        use strict; use warnings; use Data::Dumper; $Data::Dumper::Terse = 1; # Raw structures, no $VAR1 style names. # One hash, with a (hash) reference taken to it– my %stuff = ( somekey => "someval", o => "hai" ); my $stuff_ref = \%stuff; # reference literally *references* original. print "Same hash, two ways to access: $stuff{o} eq $stuff_ref->{o}\n"; # See the hash right now. print "Original hash: ", Dumper( $stuff_ref ); my $value = delete $stuff_ref->{o}; print "Hash after deleting key o: ", Dumper( $stuff_ref ); print "Print it directly (uninitialized now): o => ", $stuff_ref->{o}, + $/; # Add a new dimension to the data– $stuff_ref->{new_key}{sub_key} = "new value"; # Give it a friend, old key/value, new position– $stuff_ref->{new_key}{o} = $value; print "Hash after moving stuff around: ", Dumper( $stuff_ref ); $stuff_ref->{as}{deep}{as}{you} = "please"; $stuff_ref->{as}{deep}{as}{array_ref} = [ "one", "two" ]; $stuff_ref->{subroutine} = sub { print "OHAI!\n" }; $stuff_ref->{fancy_data} = [ "one", { two => "is a hash(ref)" }, ["ano +ther","array"], "last" ]; print "Perl data structures are *flexible* ", Dumper( $stuff_ref ); print "Excecuting code ref through data..."; $stuff_ref->{subroutine}->();
        (Update: I'm not missing <ins>you</ins> at all… now.)