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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:37:05+00:00 2026-05-12T10:37:05+00:00

I’m not sure exactly how to explain this, so I’ll just start with an

  • 0

I’m not sure exactly how to explain this, so I’ll just start with an example.

Given the following data:

Apple
Apricot
Blackberry
Blueberry
Cherry
Crabapple
Cranberry
Elderberry
Grapefruit
Grapes
Kiwi
Mulberry
Nectarine
Pawpaw
Peach
Pear
Plum
Raspberry
Rhubarb
Strawberry

I want to generate an index based on the first letter of my data, but I want the letters grouped together.

Here is the frequency of the first letters in the above dataset:

   2 A
   2 B
   3 C
   1 E
   2 G
   1 K
   1 M
   1 N
   4 P
   2 R
   1 S

Since my example data set is small, let’s just say that the maximum number to combine the letters together is 3. Using the data above, this is what my index would come out to be:

A B C D-G H-O P Q-Z

Clicking the “D-G” link would show:

Elderberry
Grapefruit
Grapes

In my range listing above, I am covering the full alphabet – I guess that is not completely neccessary – I would be fine with this output as well:

A B C E-G K-N P R-S

Obviously my dataset is not fruit, I will have more data (around 1000-2000 items), and my “maximum per range” will be more than 3.

I am not too worried about lopsided data either – so if I 40% of my data starts with an “S”, then S will just have its own link – I don’t need to break it down by the second letter in the data.

Since my dataset won’t change too often, I would be fine with a static “maximum per range”, but it would be nice to have that calculated dynamically too. Also, the dataset will not start with numbers – it is guaranteed to start with a letter from A-Z.

I’ve started building the algorithm for this, but it keeps getting so messy I start over. I don’t know how to search google for this – I’m not sure what this method is called.

Here is what I started with:

#!/usr/bin/perl

use strict;
use warnings;

my $index_frequency = { map { ( $_, 0 ) } ( 'A' .. 'Z' ) };
my $ranges = {};

open( $DATASET, '<', 'mydata' ) || die "Cannot open data file: $!\n";

while ( my $item = <$DATASET> ) {
    chomp($item);
    my $first_letter = uc( substr( $item, 0, 1 ) );
    $index_frequency->{$first_letter}++;
}

foreach my $letter ( sort keys %{$index_frequency} ) {
    if ( $index_frequency->{$letter} ) {

        # build $ranges here
    }
}

My problem is that I keep using a bunch of global variables to keep track of counts and previous letters examined – my code gets very messy very fast.

Can someone give me a step in the right direction? I guess this is more of an algorithm question, so if you don’t have a way to do this in Perl, pseudo code would work too, I guess – I can convert it to Perl.

Thanks in advance!

  • 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-12T10:37:05+00:00Added an answer on May 12, 2026 at 10:37 am

    Basic approach:

    #!/usr/bin/perl -w
    use strict;
    use autodie;
    
    my $PAGE_SIZE = 3;
    my %frequencies;
    
    open my $fh, '<', 'data';
    while ( my $l = <$fh> ) {
        next unless $l =~ m{\A([a-z])}i;
        $frequencies{ uc $1 }++;
    }
    close $fh;
    
    my $current_sum = 0;
    my @letters     = ();
    my @pages       = ();
    
    for my $letter ( "A" .. "Z" ) {
        my $letter_weigth = ( $frequencies{ $letter } || 0 );
    
        if ( $letter_weigth + $current_sum > $PAGE_SIZE ) {
            if ( $current_sum ) {
                my $title = $letters[ 0 ];
                $title .= '-' . $letters[ -1 ] if 1 < scalar @letters;
                push @pages, $title;
            }
            $current_sum = $letter_weigth;
            @letters     = ( $letter );
            next;
        }
        push @letters, $letter;
        $current_sum += $letter_weigth;
    }
    if ( $current_sum ) {
        my $title = $letters[ 0 ];
        $title .= '-' . $letters[ -1 ] if 1 < scalar @letters;
        push @pages, $title;
    }
    
    print "Pages : " . join( " , ", @pages ) . "\n";
    

    Problem with it is that it outputs (from your data):

    Pages : A , B , C-D , E-J , K-O , P , Q-Z
    

    But I would argue this is actually good approach 🙂 And you can always change the for loop into:

    for my $letter ( sort keys %frequencies ) {
    

    if you need.

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

Sidebar

Related Questions

I have some data like this: 1 2 3 4 5 9 2 6
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have just tried to save a simple *.rtf file with some websites and
I have a JSP page retrieving data and when single or double quotes are
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm looking for suggestions for debugging... If you view this site in Firefox or
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I want use html5's new tag to play a wav file (currently only supported
I want to count how many characters a certain string has in PHP, but

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.