in reply to Creating table from file lists

( Just kidding!! :)

#!/usr/bin/perl use strict; use warnings; my $h; while (<DATA>) { chomp; s|^/|\$h->{'|; s|/|'}{'|g; s|([^']*)$|\0$1'} = 1;|; eval; } use Data::Dumper; $Data::Dumper::Indent = 1; $Data::Dumper::Sortkeys = 1; open my $savedout, ">&STDOUT" or die $!; close STDOUT; open STDOUT, ">", \my $dump or die $!; print Dumper $h; open STDOUT, ">&", $savedout or die $!; open my $fh, "<", \$dump or die $!; while (<$fh>) { s/^(\s*)'/$1/; s/' =>.*//; next if /^\$|\s*}/; print; } __DATA__ /a/f1.mp3 /a/f2.mp3 /a/b/f1.mp3 /a/b/f2.mp3 /a/b/f3.mp3 /a/c/f1.mp3 /a/c/f2.mp3 /a/c/e/f5.mp3 /a/c/e/f6.mp3 /b/d/f4.mp3

Output:

a f1.mp3 f2.mp3 b f1.mp3 f2.mp3 f3.mp3 c f1.mp3 f2.mp3 e f5.mp3 f6.mp3 b d f4.mp3

Replies are listed 'Best First'.
Re^2: Creating table from file lists
by jakobi (Pilgrim) on Oct 05, 2009 at 16:38 UTC

    I re-kid you, Almut: eval, untainted, unrestricted??

    Could you do a quick replace of <DATA> with <>, with piped input from find for me, and then permit me a run of a single perl -e. Just before you execute your script?

    :>>

    (ok, it's _DATA_ and thus controlled, but it still hurts on reading. And worse, that one is pure evil to both speakers of Perl and non-speakers alike)

    Update: to restrict the hurt to mere aesthtics, consider this safer version of the input loop, which will also survive pasted arbitrary absolute pathes from DATA/or a switch from DATA to STDIN.

    my @f=(); # PJ while (<DATA>) { chomp; s|([^/]+)|do{$f[$#f+1]=$1,'$f['.$#f.']'}|ge; # PJ s|^/|\$h->{|; s|/|}{|g; s|$|} = 1;|; # PJ #print; print "\n"; eval; }

    food for thought

    1. make 'tokenization' of filenames reuse 'tokens'
    2. fix the loop to allow relative filenames (too trivial)
    3. portability: also allow c:\data\no-such.fil
    4. not a task, but an observation: use of Data::Dumper shows nicely the data structure of the recursive anonymous hash almut uses. This also demonstrates Perl's auto-vivication of the anonymous hashes we use hre: there's not one assignment of {}:
      $h->{$f[19]}{$f[20]}{$f[21]}{$f[22]} = 1;
    5. a remark: normally you'd go for recursive functions when parsing a single path into a list of -> tree nodes. Or for a slightly less readable iterative 'unrolled' version of the same (keywords tail-recursion, loop-unrolling). The above code actually can be seen as such: everything but the s|/|}{|g; has been moved outside the loop, with the loop being reduced to just a looping regex substitution (/g modifier). Not much of a loop, not much for readability, and definitely not a sane nor a correct answer to give :)
        ...definitely not a sane nor a correct answer to give :)

        Heh ;)

        To my excuse I'd like to say that after a couple of hours Java coding this afternoon, I just had to do something real ugly for a change... to wake up my brain cells.  I hope you can forgive my sins...

          Don't worry about me, I didn't test-run your code using live pathological filename specimens :).

          But I'm slightly more worried about the opener.

          Maybe we were enjoying Perl a bit too much: While there certainly is enough stuff for meditation and keywords to lookup to answer to the faithful, we have buried the answers a bit deeper than necessary for mere homework-copy-paste-reply evasion.

          But then I'm telling myself that the affc will ask more specific questions and show his own code, after he _did_ meditate and check the keywords from mickep76's recursion hint onward.

          cu
          Peter

          Update: Just found this one via RAT: id:/501932, which is a nice variation of the topic.