in reply to Re^8: (OT) Perl and creating a query for MongoDB
in thread (OT) Perl and creating a query for MongoDB

What is the code in the prase_file() subroutine ?

poj

Replies are listed 'Best First'.
Re^10: (OT) Perl and creating a query for MongoDB
by ovedpo15 (Pilgrim) on Aug 02, 2019 at 20:42 UTC
    code:
    sub prase_results { my ($file_path,$aref) = @_; open(my $fh, '<', "$file_path") or return 0; while (my $line = <$fh>) { chomp($line); my ($key,$group,$value,$version,$file,$count) = split(/,/,$lin +e); my $key = 1; foreach my $k (@{$aref}) { if ($k->{"group"} eq $group) { $k->{"version"} = [ sort uniq $version, @{$k->{"version"}} ]; $key = 0; } } push @{ $aref }, { group => $group, versions => [ $version ] } if ($key); } close ($fh); return 1; }
    By the way, I opened a new thread so it will be clear and clean. Thank you for the help.
      it will be clear ..

      It's not clear to me how this code

      foreach my $k (@{$aref}) { if ($k->{"group"} eq $group) { $k->{"version"} = [ sort uniq $version, @{$k->{"version"}} ]; $key = 0; } } push @{ $aref }, { group => $group, versions => [ $version ] } if ($key);

      can create this data structure

      { "group": "pkgs", "values": [ "tcsh" ] },

      Is the key 'version', 'versions' or 'values' ?

      poj
        Oh sorry, the code is: (too many changes and I was looking at the old version)
        open(my $fh, '<', "$file_path") or return 0; while (my $line = <$fh>) { chomp($line); my ($key,$group,$value,$version,$file,$count) = split(/,/,$lin +e); my $key = 1; foreach my $k (@{$aref}) { if ($k->{"group"} eq $group) { $k->{"values"} = [ sort uniq $value, @{$k->{"values"}} ]; $key = 0; } } push @{ $aref }, { group => $group, values => [ $value ] } if ($key); } close ($fh);