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

  • SEARCH
  • Home
  • 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 662873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:25:35+00:00 2026-05-13T23:25:35+00:00

I’m having trouble translating a subroutine from Perl to PHP (I’m new to Perl).

  • 0

I’m having trouble translating a subroutine from Perl to PHP (I’m new to Perl).
The entire subroutine is as follows:

sub find_all_subsets {
  if (1 == scalar (@_)) {return [@_]}
  else {
    my @all_subsets = () ;
    my $last_item = pop (@_) ;
    my @first_subsets = find_all_subsets (@_) ;
    foreach my $subset (@first_subsets) {
      push (@all_subsets, $subset) ;
      my @ext_subset = @{$subset} ;
      push (@ext_subset, $last_item) ;
      push (@all_subsets, [@ext_subset]) ;
    }
    push (@all_subsets, [$last_item]) ;
    return (@all_subsets) ;
  }
}

My problem is that I really don’t quite understand the Perl syntax, so I’m having trouble writing these @{$subset}, [@ext_subset] and [$last_item] in PHP.

Thanks and sorry if the question is stupid.

  • 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-13T23:25:36+00:00Added an answer on May 13, 2026 at 11:25 pm

    When I run

    use Data::Dumper;
    $Data::Dumper::Terse = 1;
    $Data::Dumper::Indent = 0; 
    my @x = (1,2,3,4);
    my @y = find_all_subsets(@x);
    foreach my $subset (@y) {
      print Dumper($subset), "\n";
    }
    

    (+ the original script) the output is

    [1]
    [1,4]
    [1,3]
    [1,3,4]
    [1,2]
    [1,2,4]
    [1,2,3]
    [1,2,3,4]
    [2]
    [2,4]
    [2,3]
    [2,3,4]
    [3]
    [3,4]
    [4]
    

    Please note the [ ] in the output, we’ll come back to them later.

    function find_all_subsets (array $x) {
      if ( 1>= count($x) ) { // the >= differs from the original script, use == or === if you want to keep it "more original"
        return array($x);
      } 
      else {
        $all_subsets = array();
        $last_item = array_pop($x);
        $first_subsets = find_all_subsets($x) ;
        foreach ($first_subsets as $subset) {
          array_push($all_subsets, $subset);
          array_push($subset, $last_item);
          array_push($all_subsets, $subset);
        }
        array_push ($all_subsets, array($last_item));
        return $all_subsets;
      }
    }
    
    $x = array(1,2,3,4);
    $y = find_all_subsets($x);
    foreach($y as $subset) {
      echo '(', join(',', $subset), ")\n";
    }
    

    produces

    (1)
    (1,4)
    (1,3)
    (1,3,4)
    (1,2)
    (1,2,4)
    (1,2,3)
    (1,2,3,4)
    (2)
    (2,4)
    (2,3)
    (2,3,4)
    (3)
    (3,4)
    (4)
    

    so far so good. Now back to the [ ]. Data::Dumper chose this [ ] and not ( ) beacuse it’s not an array but an array-reference (bare me if I don’t use the correct terms; perl is really not my strong suit). Let’s change the perl test script and look at the effect all those reference thingies have.

    use Data::Dumper;
    $Data::Dumper::Terse = 1;
    $Data::Dumper::Indent = 0; 
    $x2 = 2;
    my @x = (1,\$x2,3,4);
    my @y = find_all_subsets(@x);
    $x2 = 99;
    foreach my $subset (@y) {
      print Dumper($subset), "\n";
    }
    

    and the output changes to

    [1]
    [1,4]
    [1,3]
    [1,3,4]
    [1,\99]
    [1,\99,4]
    [1,\99,3]
    [1,\99,3,4]
    [\99]
    [\99,4]
    [\99,3]
    [\99,3,4]
    [3]
    [3,4]
    [4]
    

    You see, I change $x2 after the call to find_all_subsets() but still in the result the new value is used and Data::Dumper marks the “value” as a reference ( \99 instead of simply 99 ). Do you need this feature in your php script, too?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.