in reply to Parsing output from a 'system' call into objects?

A more basic example

use warnings; use strict; #my $testout=`$test list`; my $testout=' CoID: Type: State: ID: ExampleCo A former 97546 ExampleCo B pending 48541 ExampleCo A ready 75521 ExampleCo B former 50123 ExampleCo A contact 60047 ExampleCo B contact 19425 '; my @order; my $array={}; my @sparse; my @compact; for my $l (split("\n",$testout)) { chomp $l; my ($coid,$type,$state,$id)=split(' ',$l,4); next unless ($type eq 'A'); my $h={ID=>$id,CoID=>$coid,State=>$state}; push @order,$id; $array->{$id}=$h; $sparse[$id]=$h; push @compact,$h; } print 'From hash'."\n"; for my $o (@order) { my $h=$array->{$o}; print ' ID = '.$h->{ID}."\n"; print ' CoID = '.$h->{CoID}."\n"; print ' State = '.$h->{State}."\n"; } print 'iterate compact'."\n"; for my $h (@compact) { next unless (defined $h); print ' ID = '.$h->{ID}."\n"; print ' CoID = '.$h->{CoID}."\n"; print ' State = '.$h->{State}."\n"; } print 'index sparse'."\n"; for my $o (@order) { my $h=$sparse[$o]; print ' ID = '.$h->{ID}."\n"; print ' CoID = '.$h->{CoID}."\n"; print ' State = '.$h->{State}."\n"; }
result
From hash ID = 97546 CoID = ExampleCo State = former ID = 75521 CoID = ExampleCo State = ready ID = 60047 CoID = ExampleCo State = contact iterate compact ID = 97546 CoID = ExampleCo State = former ID = 75521 CoID = ExampleCo State = ready ID = 60047 CoID = ExampleCo State = contact index sparse ID = 97546 CoID = ExampleCo State = former ID = 75521 CoID = ExampleCo State = ready ID = 60047 CoID = ExampleCo State = contact
This example does not need IPC::System::Simple to be preinstalled, uses the `$test` call format that you implied, captures only the type=a records, and shows it stored in a hash, compact array and sparse array, iterating over the compact array and indexing the sparse array.

if you prefer to install IPC::System::Simple that is a much prettier program (edit:) and you add the test for type=a

Replies are listed 'Best First'.
Re^2: Parsing output from a 'system' call into objects?
by stevieb (Canon) on Feb 21, 2017 at 20:09 UTC

    For your information, most (if not all) of your print statements can be interpolated. So this:

    print 'From hash'."\n"; for my $o (@order) { my $h=$array->{$o}; print ' ID = '.$h->{ID}."\n"; print ' CoID = '.$h->{CoID}."\n"; print ' State = '.$h->{State}."\n";

    Becomes this:

    print "From hash\n"; for my $o (@order) { my $h=$array->{$o}; print " ID = $h->{ID}\n"; print " CoID = $h->{CoID}\n"; print " State = $h->{State}\n";

    Or:

    print "From hash\n"; for my $o (@order) { print "" . " ID = $array->{$o}->{ID}\n" . " CoID = $array->{$o}{CoID}\n" . " State = $array->{$o}->{State}\n";

    Or with a heredoc, but I digress.

    Also, I'm curious as to why you're calling a hash $array... ;)

      What no example this time to prove it? I thought you always wanted to see examples

      I prefer to make it clear when a value comes from a variable rather than infer it does via interpolation. It also means it is clearer when i say

      my $thing='dog'; print 'many '.$thing.'s'."\n";
      rather than the more obscure
      my $thing='dog'; print "many ${thing}s\n";
      without getting caught by
      my $thing='dog'; print "many $things\n";
      Being clear is a good habit i started about 4 decades ago, after trying to read other peoples APL code. I mostly say my $n=scalar(@array); for clarity too.

      You are right about calling it $array, it started because eval142 said he wanted an indexed array of objects.