Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re^2: Variable number of foreach loops (non-recursive solution)

by abhay180 (Acolyte)
on Nov 29, 2013 at 09:48 UTC ( #1064930=note: print w/replies, xml ) Need Help??

in reply to Re: Variable number of foreach loops (non-recursive solution)
in thread Variable number of foreach loops

Thanks a lot. That helps.
  • Comment on Re^2: Variable number of foreach loops (non-recursive solution)

Replies are listed 'Best First'.
Re^3: Variable number of foreach loops (performance)
by BrowserUk (Pope) on Nov 29, 2013 at 11:23 UTC

    Just in case performance is a consideration:

    #! perl -slw use strict; use Algorithm::Loops qw[ NestedLoops ]; use Time::HiRes qw[ time ]; sub nFor(&@) { my $code = shift; my @indices = map { 0 } @_; # First set of indices is all zeroes my @sizes = map { scalar @$_ } @_; # Cache array sizes (optional) my $k; do { # Determine the array elements corresponding to the current se +t # of indices, and pass them to the closure: $code->( map { $_[$_][$indices[$_]] } 0..$#_ ); # Determine the next set of indices: for ($k = $#_; $k >= 0; $k--) { $indices[$k]++; if ($indices[$k] < $sizes[$k]) { last; } else { $indices[$k] = 0; } } # If $k went out-of-bounds, it means we're finished: } while ($k >= 0); } sub nForX(&@) { my $code = shift; my $n = shift; return $code->( @_ ) unless $n; for my $i ( @{ shift() } ) { &nForX( $code, $n-1, @_, $i ); } } my %stuff = ( A => [ 1..1000 ], B => [ 'a'..'z', 'A'..'Z' ], C => [ map chr, 33..47, 58..64, 92..96 ], ); my $start; for my $pat ( qw[ A::B A::C B::C A::B::C ] ) { print "\nProcessing $pat"; my @keys = split '::', $pat; $start = time; nForX { my @set = @_; } scalar @keys, @stuff{ @keys }; printf "\tRecursive: %f seconds\n", time - $start; $start = time; nFor { my @set = @_; } @stuff{ @keys }; printf "\tIterative: %f seconds\n", time - $start; $start = time; NestedLoops [ @stuff{ @keys } ], sub { my @set = @_; }; printf "\tNestedLoops %f seconds\n", time - $start; } __END__ C:\test>nforx Processing A::B Recursive: 0.107126 seconds Iterative: 0.227112 seconds NestedLoops 0.474461 seconds Processing A::C Recursive: 0.049802 seconds Iterative: 0.117263 seconds NestedLoops 0.235748 seconds Processing B::C Recursive: 0.002990 seconds Iterative: 0.006834 seconds NestedLoops 0.014829 seconds Processing A::B::C Recursive: 3.072954 seconds Iterative: 7.725672 seconds NestedLoops 15.938471 seconds

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1064930]
[Corion]: But yes, "who started this process" is interesting information :)
[tye]: no, I really believe that "login user" was added as a fundamental bit of info about each process in order to enhance the usefulness of auditing
[Corion]: Ah - if that information is saved in a file, then you could theoretically spam that file and confuse getlogin(). So, don't use it for authentication :)
[tye]: that is what getlogin() certainly *used* to do. I don't believe that is what it certainly should do.
[davido]: /var/run/utmp is 664 i think.
[tye]: Note that my "man getlogin" says that it uses stdin when it should use /dev/tty (calling a glibc bug). But that does not appear to be the case when I test it. But maybe Perl's getlogin() is not using glibc's getlogin().
[oiskuu]: well, run a strace and see what the getlogin does for you.... As I said. SELinux probably has those security labels. But not regular linux.
[tye]: for example, read https://unix. questions/146138/ loginuid-should-be -allowed-to-change -or-not-mutable-or -not
[tye]: I'm not using SELinux and it certainly appears to disagree with you. shrug
[tye]: Since you brought up /proc, oiskuu, I didn't see you respond to my suggestion of 'loginuid'. Does your /proc not have such?

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2017-06-23 19:46 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (554 votes). Check out past polls.