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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:12:40+00:00 2026-05-24T17:12:40+00:00

I have an input file that looks like *firsttitle nameA nameB nameC *secondtitle xnameA

  • 0

I have an input file that looks like

*firsttitle
nameA
nameB
nameC
*secondtitle
xnameA
xnameB
xnameC

I want to create a Perl script that takes this file and basically will create another perl script that looks like

  #!/usr/bin/perl

        use strict;
        use warnings;

        my %tags = (
            "firsttitle" => [ qw (nameA nameB nameC) ],
            "secondtitle" => [ qw (xnameA xnameB xnameC) ]);

 my $rx = join '|', keys %tags;

 while (<>) {
    s/^\s*($rx):\s*(\d+)/$1: $tags{$1}[$2]/;
    print;
 }

My thought process is that I have to first match print out the regular perl code (#!,use..etc.).Then add ” my%tags=(. Then take the input file and look for the * and that’s the lookup for the hash and start parsing everything after until the next(*) or end of life. If it’s another * then do it again. If it’s EOF then add “);” and end. And then finish with printing the last bit of perl code. Help/ideas would be appreciated. If you’re going to post code snippets could you go through and explain what each part is doing? Thanks!

  • 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-24T17:12:41+00:00Added an answer on May 24, 2026 at 5:12 pm

    Very simple script. First just parse through the input file. Lines that start with * will be titles, and all the following lines up until the next *-line will be values. We put this into a hash of arrays.

    The map statement gives us a list of the hash key (the title), and it’s values joined together with space. We put this in an array for printing. The printing itself is done with printf, which can be a bit difficult to use, since meta characters will mess us up. Any % that are to be literal must be written as %%. I also changed single quotes from the original to double quotes. I use single quotes on the printf pattern to avoid accidental interpolation of variables.

    An alternative – possibly better one – is to not just printf at all, and simply concatenate the string in a normal fashion.

    use strict;
    use warnings;
    
    my ($title, %hash);
    while (<DATA>) {
        chomp;
        if (/^\*(.+)$/) {
            $title = $1;
        } else {
            push @{$hash{$title}}, $_;
        }
    }
    
    my @args = ( map { $_, join(' ', @{$hash{$_}}) } keys %hash );
    printf '#!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my %%tags = (
            "%s" => [ qw ( %s ) ],
            "%s" => [ qw ( %s ) ]);
    
    my $rx = join "|", keys %%tags;
    
    while (<>) {
        s/^\s*($rx):\s*(\d+)/$1: $tags{$1}[$2]/;
        print;
    }', @args;
    
    __DATA__
    *firsttitle
    nameA
    nameB
    nameC
    *secondtitle
    xnameA
    xnameB
    xnameC
    

    Update:

    This will use a different method of printing, which will be more stable.

    my @args = ( map { "    '$_' => [ qw ( @{$hash{$_}} ) ],\n" } keys %hash );
    print '#!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my %tags = (
    ', @args, '
    );
    
    my $rx = join "|", keys %tags;
    
    while (<>) {
        s/^\s*($rx):\s*(\d+)/$1: $tags{$1}[$2]/;
        print;
    }';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi, I have a input that looks like this : <input type=file id=ModelViewAd.Files[0] name=ModelViewAd.Files[0]
I have some input file that looks like this: asdfasfasfas.... asdfasdfasd.... asdfasdf.... asdfsadf... I
The problem I have a tab delimited input file that looks like so: Variable
I have an input file that I want to sort based on timestamp which
I have a file that looks like this(have to put in code box so
So I have a a text file that looks something like this: public >ret
Let's say I have an XML file that looks like this: <a id=x> <b>
I have an xml file that looks like this: <args> <sometag value=abc /> <anothertag
I have a tab-delimited file that looks something like this: \>WB02 \t F27C8.1 IV
I'm using getline to grab lines from an input data file that looks like

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.