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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:24:57+00:00 2026-06-06T23:24:57+00:00

Well.. I’m stuck again. I’ve read up quite a few topic with similar problems

  • 0

Well.. I’m stuck again. I’ve read up quite a few topic with similar problems but not finding a solution for mine. I have a ; delimited csv file and the strings at the 8th column ($elements[7]) is as following: “aaaa;bb;cccc;ddddd;eeee;fffff;gg;”. What i’m trying is to split the string based on ; and capture the outputs to variables. Then use those variables in the main csv file in their own column.

So now the file is like:

3d;2f;7j;8k;4s;2b;5g;”aaaa;bb;cccc;ddddd;eeee;fffff;gg;”;4g;1a;5g;2g;7h;3d;2f;7j
3c;9k;5l;4g;1a;5g;3d;”aaaa;bb;cccc;ddddd;eeee;fffff;gg;”;4g;1a;5g;2g;7h;3d;2f;7j
4g;1a;5g;2g;7h;3d;8k;”aaaa;bb;cccc;ddddd;eeee;fffff;gg;”;3d;2f;7j;8k;4s;2b;4g;1a

And i want it like:

3d;2f;7j;8k;4s;2b;5g;4g;1a;5g;2g;7h;3d;2f;7j;aaaa;bb;cccc;ddddd;eeee;fffff;gg
3c;9k;5l;4g;1a;5g;3d;4g;1a;5g;2g;7h;3d;2f;7j;aaaa;bb;cccc;ddddd;eeee;fffff;gg;
4g;1a;5g;2g;7h;3d;8k;3d;2f;7j;8k;4s;2b;4g;1a;aaaa;bb;cccc;ddddd;eeee;fffff;gg;

This is my code i’ve been trying it with. I know.. it’s terrible! But i’m hoping someone can help me?

use strict;
use warnings;

my $inputfile  = shift || die "Give files\n";
my $outputfile = shift || die "Give output\n";

open my $INFILE,  '<', $inputfile   or die "In use / Not found :$!\n";
open my $OUTFILE, '>', $outputfile  or die "In use :$!\n";

while (<$INFILE>) {
  s/"//g;
  my @elements = split /;/, $_;

    my ($varA, $varB, $varC, $varD, $varE, $varF, $varG, $varH) split (';', $elements[10]);
        $elements[16] = $varA;
        $elements[17] = $varB;
        $elements[18] = $varC;
        $elements[19] = $varD; 
        $elements[20] = $varE;
        $elements[21] = $varF;
        $elements[22] = $varG;
        $elements[23] = $varH;

my $output_line = join(";", @elements);
print $OUTFILE $output_line;
}

close $INFILE;
close $OUTFILE;

exit 0;

I’m confused about the my statement as well, it shouldn’t be possible right? I mean the $vars are in a closed part so it shouldn’t be possible to write them to $elements?

EDIT

This is how i adjusted the code with TLP’s suggestions:

use strict;
use warnings;
use Text::CSV;

my $inputfile  = shift || die "Give files\n";
my $outputfile = shift || die "Give output\n";

open my $INFILE,  '<', $inputfile   or die "In use / Not found :$!\n";
open my $OUTFILE, '>', $outputfile  or die "In use :$!\n";

my $csv = Text::CSV->new({  # create a csv object
    sep_char => ";",    # delimiter
    eol => "\n",        # adds newline to print
});

while (my $row = $csv->getline($INFILE)) {      # $row is an array ref
my $line = splice(@$row, 10, 1);            # remove 8th line
$csv->parse($line);                         # parse the line
push @$row, $csv->fields();                 # push newly parsed fields onto       main array
$csv->print($OUTFILE, $row);
}

close $INFILE;
close $OUTFILE;

exit 0;
  • 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-06T23:24:58+00:00Added an answer on June 6, 2026 at 11:24 pm

    You should use a CSV module, e.g. Text::CSV to parse your data. Here’s a brief example on how it can be done. You can replace the file handles I used below with your own.

    use strict;
    use warnings;
    use Text::CSV;
    
    my $csv = Text::CSV->new({     # create a csv object
            sep_char => ";",   # delimiter
            eol => "\n",       # adds newline to print
        });
    
    while (my $row = $csv->getline(*DATA)) {   # $row is an array ref
        my $line = splice(@$row, 7, 1);   # remove 8th line
        $csv->parse($line);               # parse the line
        push @$row, $csv->fields();       # push newly parsed fields onto main array
        $csv->print(*STDOUT, $row);
    }
    
    __DATA__
    3d;2f;7j;8k;4s;2b;5g;"aaaa;bb;cccc;ddddd;eeee;fffff;gg;";4g;1a;5g;2g;7h;3d;2f;7j
    3c;9k;5l;4g;1a;5g;3d;"aaaa;bb;cccc;ddddd;eeee;fffff;gg;";4g;1a;5g;2g;7h;3d;2f;7j
    4g;1a;5g;2g;7h;3d;8k;"aaaa;bb;cccc;ddddd;eeee;fffff;gg;";3d;2f;7j;8k;4s;2b;4g;1a
    

    Output:

    3d;2f;7j;8k;4s;2b;5g;4g;1a;5g;2g;7h;3d;2f;7j;aaaa;bb;cccc;ddddd;eeee;fffff;gg;
    3c;9k;5l;4g;1a;5g;3d;4g;1a;5g;2g;7h;3d;2f;7j;aaaa;bb;cccc;ddddd;eeee;fffff;gg;
    4g;1a;5g;2g;7h;3d;8k;3d;2f;7j;8k;4s;2b;4g;1a;aaaa;bb;cccc;ddddd;eeee;fffff;gg;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well normally I am quite good at figuring and researching problems without guidance however
Well this question and this question are similar but no answers that work. In
Well, my website can not redirect to https://www.facebook.com/QuaFootSpa from http://quafootspa.com/ I have tried redirection
Well, I have a application which somehow requires some system resources, but how do
Well it's not realy causing any errors. Anyways i'm sending ajax request and getting
Well I'm making an program about payrolls and I'm stuck. In the program, after
Well I created a jquery mobile form but the every element is in a
Well, I know there is a lots of posts about it, but I have
Well, that might be a strange question, and maybe just because I'm not familiar
Well, i have my issues with gluCylinder(), it's not letting me do the things.

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.