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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T16:26:13+00:00 2026-05-14T16:26:13+00:00

I have an SQL file which will give me an output like below: 10|1

  • 0

I have an SQL file which will give me an output like below:

10|1
10|2
10|3
11|2
11|4
.
.
.

I am using this in a Perl script like below:

my @tmp_cycledef = `sqlplus -s $connstr \@DLCycleState.sql`;

after this above statement, since @tmp_cycledef has all the output of the SQL query,
I want to show the output as:

10 1,2,3
11 2,4

How could I do this using Perl?

EDIT:

I am using the following code:

foreach  my $row (@tmp_cycledef)
{
        chomp $row;
        my ($cycle_code,$cycle_month)= split /\s*\|\s*/, $row;
        print "$cycle_code, $cycle_month\n";
        $hash{$cycle_code}{$cycle_month}=1
}

foreach my $num ( sort keys %hash )
{
        my $h = $hash{$num};
        print join(',',sort keys %$h),"\n";
}

the fist print statement prints:

     2, 1
     2, 10
     2, 11
     2, 12
     3, 1
     3, 10
     3, 11

but the out is always

1,10,11,12
1,10,11,12
1,10,11,12
1,10,11,12
1,10,11,12
1,10,11,12
1,10,11,12
  • 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-14T16:26:14+00:00Added an answer on May 14, 2026 at 4:26 pm

    Well, this one is actually how you might do it in perl:

    # two must-have pragmas for perl development
    use strict;    
    use warnings;
    

    Perl allows for variables to be created as they are used, $feldman = some_function() means that you now have the variable $feldman in your local namespace. But the bad part about this is that you can type $fldman and take a long time finding out why what you thought was $feldman has no value. Turning on strictures means that your code fails to compile if it encounters an undeclared variable. You declare a variable with a my or our statement (or in older Perl code a use vars statement.

    Turning on warnings just warns you when you’re not getting values you expect. Often warnings tends to be too touchy, but they are generally a good thing to develop code with.

    my %hash; # the base object for the data
    

    Here, I’ve declared a hash variable that I creatively called %hash. The sigil (pronounced “sijil”) “%” tells that it is a map of name-value pairs. This my statement declared the variable and makes it legal for the compiler. The compiler will warn me about any use of %hsh.

    The next item is a foreach loop (which can be abbreviated “for”). The loop will process the list of lines in @tmp_cycledef assigning each one in turn to $row. ( my $row).

    1. We chomp the line first, removing the end-of-line character for that platform.
    2. We split the line on the ‘|’ character, creating a list of strings that had been separated by a pipe.
    3. And then we store it in a two-layered hash. Since we want to group them by at least the first number. We could do this by array, and create an array at the location in the hash like so: push @{$hash{$key}}, $val, but I typically want to collapse duplicates (not that there were any duplicates in your sample.)

    Here:

    foreach my $row ( @tmp_cycledef ) { 
        chomp $row; # removes the end-of-line character when present. 
        my ( $key, $val ) = split /\|/, $row; 
        # One of the best ways to merge lists is a presence-of idea
        # with the hash holding whether the value is present
        $hash{$key}{$val} = 1; 
    }
    

    Once we have the data in the structure, we need to iterate both level of hash keys. You wanted to separate the “top level” numbers by lines, but you wanted the second numbers concatenated on the same line. So we print a line for each of the first numbers and join the list of strings stored for each number on the same line, delimited by commas. We also sort the list: { $a <=> $b } just takes to keys and numerically compares them. So you get a numeric order.

    # If they were alpha keys our sort routine, we would just likely say sort keys %hash
    foreach my $num ( sort { $a <=> $b } keys %hash ) { 
        my $h = $hash{$num};
        print "$num ", join( ',', sort { $a <=> $b } keys %$h ), "\n";
    }
    

    As I said in the comments, sort, by default, sorts in character order so you can just say sort keys %hash.

    To help you out, you really need to read some of these:

    • strictures
    • warnings
    • perldata
    • perlfunc — especially my, foreach, chomp, split, keys, sort and join
    • And the data structure tutorial
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 411k
  • Answers 411k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You could use a properties file as you mentions just… May 15, 2026 at 7:40 am
  • Editorial Team
    Editorial Team added an answer You can use DevExpress. May 15, 2026 at 7:40 am
  • Editorial Team
    Editorial Team added an answer Using BETWEEN with YEAR() and MONTH() is going to ruin… May 15, 2026 at 7:40 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.