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 6138525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:52:45+00:00 2026-05-23T17:52:45+00:00

I have an array with this type of content a/a/a/test134.html a/a/a/test223.html a/b/b/test37.html a/b/test41.html a/b/test44.html

  • 0

I have an array with this type of content

a/a/a/test134.html
a/a/a/test223.html
a/b/b/test37.html
a/b/test41.html
a/b/test44.html
a/b/test432.html
a/d/test978.html
a/test.html

I need to split it by “directories” so that I can send each array for a directory into a function (please see code sample).

a/a/a/test134.html
a/a/a/test223.html

a/b/b/test37.html

a/b/test41.html
a/b/test44.html
a/b/test432.html

a/d/test978.html

a/test.html

This is what I have so far but I feel theres lots of bugs especially on end and beginning cases and is not clean enough to my liking.

for(my $i = 0; $i < scalar(@arrayKeys); $i++)
{
    my($filename, $directory) = fileparse($arrayKeys[$i]);
    my $currDir = $directory;

    # $currDir ne $prevDir: takes care of changes in path
    # $i + 1 == scalar(@arrayKeys): accounts for last row to be purged
    if($currDir ne $prevDir || $i + 1 == scalar(@arrayKeys))
    {   
        # if last row we need to push it
        if($i + 1 == scalar(@arrayKeys))
        {
            push(@sectionArrayKeys, $arrayKeys[$i]);
        }

        # ensure for first entry run we don't output
        if ($prevDir ne "")
        {
            &output(\@sectionArrayKeys);
        }

        # Clear Array and start new batch
        @sectionArrayKeys = ();
        push(@sectionArrayKeys, $arrayKeys[$i]);        
    }
    else
    {
        push(@sectionArrayKeys, $arrayKeys[$i]);        
    }

    $prevDir = $currDir;
}
  • 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-23T17:52:45+00:00Added an answer on May 23, 2026 at 5:52 pm

    Your script is confusing, but from what I understand, you want to split the array of paths into new arrays, depending on their path. Well, easiest way to keep them apart is using a hash, like so:

    use warnings;
    use strict;
    
    my %dir_arrays;
    
    while (<DATA>) {
        chomp;
        if (m{^(.+/)([^/]+)$}) {
            push @{$dir_arrays{$1}}, $_; # or use $2 for just filename
        }
    }
    
    use Data::Dumper;
    print Dumper \%dir_arrays;
    
    __DATA__
    a/a/a/test134.html
    a/a/a/test223.html
    a/b/b/test37.html
    a/b/test41.html
    a/b/test44.html
    a/b/test432.html
    a/d/test978.html
    a/test.html
    

    Output:

    $VAR1 = {
              'a/b/' => [
                          'a/b/test41.html',
                          'a/b/test44.html',
                          'a/b/test432.html'
                        ],
              'a/d/' => [
                          'a/d/test978.html'
                        ],
              'a/b/b/' => [
                            'a/b/b/test37.html'
                          ],
              'a/a/a/' => [
                            'a/a/a/test134.html',
                            'a/a/a/test223.html'
                          ],
              'a/' => [
                        'a/test.html'
                      ]
            };
    

    Now, to send these arrays to a function, do something like this:

    for my $key (keys %dir_arrays) {
        my_function($dir_arrays{$key}); # this sends an array reference
    }
    

    If you prefer to send an array instead of an array reference, just dereference it:

    my_function(@{$dir_arrays{$key}});
    

    Edit: Changed the script to store the full path, as it was more in line with the wanted output in the question.

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

Sidebar

Related Questions

i have this code on the server side: <?php header('Content-Type: text/html; charset=utf-8'); require ../general.variables.php;
I have an array like this: object[] args and need to insert those args
i need to serialize directory tree. i have no trouble with this type: std::map<
I have this code for an app i'm working on: <?php header(Content-type: text/xml); //Gather
I have been using this simple script to generate images from text: <?php header('Content-type:
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
I have this Array i wrote a function MostFreq that takes an array of
I have this array in PHP: array( [0] => array( 'username' => 'user1' )
I have this array, for example (the size is variable): x = [1.111, 1.122,
I have this array: string[,] productData = new string[5,7]; I bind it to a

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.