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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:40:07+00:00 2026-06-09T04:40:07+00:00

I have a text file temp.txt which contains entries like, cinterim=3534 cstart=517 cstop=622 ointerim=47

  • 0

I have a text file temp.txt which contains entries like,

cinterim=3534
cstart=517
cstop=622
ointerim=47
ostart=19
ostop=20

Note: key-value pairs may be arranged in new line or all at once in one line separated by space.

I am trying to print and store these values in DB for corresponding keys using Perl. But I am getting many errors and warnings. Right now I am just trying to print those values.

use strict;
use warnings;

open(FILE,"/root/temp.txt") or die "Unable to open file:$!\n";

while (my $line = <FILE>) {
  # optional whitespace, KEY, optional whitespace, required ':', 
  # optional whitespace, VALUE, required whitespace, required '.'
  $line =~ m/^\s*(\S+)\s*:\s*(.*)\s+\./;
  my @pairs = split(/\s+/,$line);
  my %hash = map { split(/=/, $_, 2) } @pairs;

  printf "%s,%s,%s\n", $hash{cinterim}, $hash{cstart}, $hash{cstop};

}
close(FILE);

Could somebody provide help to refine my program.

  • 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-06-09T04:40:08+00:00Added an answer on June 9, 2026 at 4:40 am
    use strict;
    use warnings;
    
    open my $fh, '<', '/root/temp.txt' or die "Unable to open file:$!\n";
    my %hash = map { split /=|\s+/; } <$fh>;
    close $fh;
    print "$_ => $hash{$_}\n" for keys %hash;
    

    What this code does:

    <$fh> reads a line from our file, or in list context, all lines and returns them as an array.

    Inside map we split our line into an array using the regexp /= | \s+/x. This means: split when you see a = or a sequence of whitespace characters. This is just a condensed and beautified form of your original code.

    Then, we cast the list resulting from map to the hash type. We can do that because the item count of the list is even. (Input like key key=value or key=value=valuewill throw an error at this point).

    After that, we print the hash out. In Perl, we can interpolate hash values inside strings directly and don’t have to use printf and friends except for special formatting.

    The for loop iterates over all keys (returned in the $_ special variable), and $hash{$_} is the corresponding value. This could also have been written as

    while (my ($key, $val) = each %hash) {
      print "$key => $val\n";
    }
    

    where each iterates over all key-value pairs.

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

Sidebar

Related Questions

I have text file with entries like 123 112 3333 44 2 How to
I have a text file which contains some locations of the files which I
I have file File1.txt in which i have to replace a text using sed
I have text file with some stuff that i would like to put into
I have text file with something like first line line nr 2 line three
I have Text file that contains data separated with a comma , . How
I have text file, like FILED AS OF DATE: 20090209 DATE AS OF CHANGE:
I' ve got a problem with Haskell. I have text file looking like this:
I have a text file and I would like to parse it using regular
I have a text file like this Path 827 196 847 195 868 194

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.