Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 745399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T09:05:25+00:00 2026-05-14T09:05:25+00:00

I have inherited some code from a guy whose favorite past time was to

  • 0

I have inherited some code from a guy whose favorite past time was to shorten every line to its absolute minimum (and sometimes only to make it look cool). His code is hard to understand but I managed to understand (and rewrite) most of it.

Now I have stumbled on a piece of code which, no matter how hard I try, I cannot understand.

my @heads = grep {s/\.txt$//} OSA::Fast::IO::Ls->ls($SysKey,'fo','osr/tiparlo',qr{^\d+\.txt$}) || ();
my @selected_heads = ();
for my $i (0..1) {
   $selected_heads[$i] = int rand scalar @heads;
   for my $j (0..@heads-1) {
      last if (!grep $j eq $_, @selected_heads[0..$i-1]);
      $selected_heads[$i] = ($selected_heads[$i] + 1) % @heads; #WTF?
   }
   my $head_nr = sprintf "%04d", $i;
   OSA::Fast::IO::Cp->cp($SysKey,'',"osr/tiparlo/$heads[$selected_heads[$i]].txt","$recdir/heads/$head_nr.txt");
   OSA::Fast::IO::Cp->cp($SysKey,'',"osr/tiparlo/$heads[$selected_heads[$i]].cache","$recdir/heads/$head_nr.cache");
}

From what I can understand, this is supposed to be some kind of randomizer, but I never saw a more complex way to achieve randomness. Or are my assumptions wrong? At least, that’s what this code is supposed to do. Select 2 random files and copy them.

=== NOTES ===

The OSA Framework is a Framework of our own. They are named after their UNIX counterparts and do some basic testing so that the application does not need to bother with that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T09:05:25+00:00Added an answer on May 14, 2026 at 9:05 am

    This looks like some C code with Perl syntax. Sometimes knowing the language the person is thinking in helps you figure out what’s going on. In this case, the person’s brain is infected with the inner workings of memory management, pointer arithmetic, and other low level concerns, so he wants to minutely control everything:

    my @selected_heads = ();
    
    # a tricky way to make a two element array
    for my $i (0..1) {
    
       # choose a random file
       $selected_heads[$i] = int rand @heads;
    
       # for all the files (could use $#heads instead)
       for my $j (0..@heads-1) {
          # stop if the chosen file is not already in @selected_heads
          # it's that damned ! in front of the grep that's mind-warping
          last if (!grep $j eq $_, @selected_heads[0..$i-1]);
    
          # if we are this far, the two files we selected are the same
          # choose a different file if we're this far
          $selected_heads[$i] = ($selected_heads[$i] + 1) % @heads; #WTF?
       }
    
    ...
    }
    

    This is a lot of work because the original programmer either doesn’t understand hashes or doesn’t like them.

    my %selected_heads;
    until( keys %selected_heads == 2 )
        {
        my $try = int rand @heads;
        redo if exists $selected_heads{$try};
        $selected_heads{$try}++;
        }
    
    my @selected_heads = keys %selected_heads;
    

    If you still hate hashes and have Perl 5.10 or later, you can use smart-matching to check if a value is in an array:

    my @selected_heads;
    until( @selected_heads == 2 )
        {
        my $try = int rand @heads;
        redo if $try ~~ @selected_heads;
        push @selected_heads, $try;
        }
    

    However, you have a special constraint on this problem. Since you know there are only two elements, you just have to check if the element you want to add is the prior element. In the first case it won’t be undef, so the first addition always works. In the second case, it just can’t be the last element in the array:

    my @selected_heads;
    until( @selected_heads == 2 )
        {
        my $try = int rand @heads;
        redo if $try eq $selected_heads[-1];
        push @selected_heads, $try;
        }
    

    Huh. I can’t remember the last time I used until when it actually fit the problem. 🙂

    Note that all of these solutions have the problem that they can cause an infinite loop if the number of original files is less than 2. I’d add a guard condition higher up so the no and single file cases through an error and perhaps the two file case doesn’t bother to order them.

    Another way you might do this is to shuffle (say, with List::Util) the entire list of original files and just take off the first two files:

    use List::Util qw(shuffle);
    
    my @input = 'a' .. 'z';
    
    my @two = ( shuffle( @input ) )[0,1];
    
    print "selected: @two\n";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have inherited some code (from zip file) from a developer and git initialzed,
I have some ASP code that I've inherited from my predecessor (no, it's not
I have some code that I've inherited from someone very clever where they like
Good afternoon, I inherited some C# code from years ago. I have refactored it
I have some code (inherited) which selects a row from table 'members' and also
I have inherited some code: Process p = new ProcessBuilder(/bin/chmod, 777, path).start(); p.waitFor(); Basically,
I have inherited some HTML code and have been asked to align the two
I have some troubles with a method having a typed List parameter, inherited from
I have inherited some code which involves a scheduled task that writes data (obtained
I have inherited a project from a previous developer. All the ASP .NET code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.