in reply to Re^3: Parsing a file and finding the dependencies in it
in thread Parsing a file and finding the dependencies in it

Can anyone help to explain what this line does?

print map{" $_\n"}grep{!$seen{$_}++}priorFiles($file);

I've read the perldoc on the map function and I think the grep{!$seen{$_}++}priorFiles($file) portion extracts unique elements and the priorFiles subroutine returns the "Start" files? Could someone explain it please?

Also, I have been trying to figure out how I would be able to tell if an "ID" or "Desc" depends on another "ID" or "Desc" such as showing ID 456 depends on ID 423 which basically entails looking up the input or "Start" files to see where (which "ID") they came from

Replies are listed 'Best First'.
Re^5: Parsing a file and finding the dependencies in it
by Anonymous Monk on Jul 09, 2011 at 15:23 UTC
Re^5: Parsing a file and finding the dependencies in it
by Marshall (Canon) on Jul 09, 2011 at 15:10 UTC
    Yes, grep{!$seen{$_}++} just removes duplicates from the list. Perl grep is filtering operation and more powerful than command line grep. It passes the input to the output if the last line of the grep evaluates to "true".

    This grep code checks the "truthness" of the seen hash entry for $_. The ! makes it a "not". So this is true if we have not seen a value before. The ++ is a post increment which happens after we've tested for existence. If the key does not already exist, Perl creates it, and allows the undef initial value to be used in the increment. The resulting value is 1 (0+1). If the key already exists it just gets incremented.

    The list returned from priorFiles is every possible file that could have affected a particular output file. It contains dupes because some of the input files will share a at least a partial ancestry. priorFiles() is a bit tricky as it calls itself. This is a recursive function and may a bit mind-bending if you haven't seen one before.

    I think you are on the way now. Play with the code, insert prints to watch what it does.