0

I am trying to parse a CSV file using perl, I need to read, add a new row (both into a empty file and to the file already consisting data) and delete the row/rows by matching a particular value, I wanted to do it using a inbuilt module only doesn't want to install a external new module.

Earlier I was trying it with using xml file but it required to install XML::Twig or XML::LibXML which I didn't want.

Is it possible through csv or text file.

5
  • 2
    Any particular reason to avoid great CPAN modules? Commented Jul 24, 2015 at 6:08
  • @MiguelPrz actually the script will be used by many users and they don't want any extra dependency so I am looking at a way so I have not to install any modules if it is possible. Commented Jul 24, 2015 at 6:11
  • 3
    I think it's better to learn how to distribute your code with external libs as part of your installation in Perl. Commented Jul 24, 2015 at 6:17
  • @MiguelPrz sorry, but I din't get what you mean ?? Commented Jul 24, 2015 at 6:19
  • 2
    Read this article: perltricks.com/article/58/2014/1/5/… Commented Jul 24, 2015 at 6:20

2 Answers 2

1

You should use Text::CSV module. If you don't want to install it then you are left with split and tricky handling.

See this tutorial by Gabor: How to read a CSV file using Perl?

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, can I perform the delete and update operation without installing the Text::CSV and if not is it possible with .txt file without installing any module.
0

You can parse CSV file into XML without installing new module. Here is how:

use warnings;
use strict;
use Data::Dumper;

my $CSVfile="customer.csv";


print "Input Csv file: $CSVfile \n";


my ($arrRef,$keys)= convertCsvToHash($CSVfile);
print Dumper($arrRef);


sub convertCsvToHash{
    my $file = shift;

    open(FILE, "$file");
    my @arr = <FILE>;
    close(FILE);

    my @hashArry;
    chomp($arr[0]);
    my @keys = split(',',$arr[0]);

    for my $i(1..$#arr){
            chomp($arr[$i]);
            my @splitRec = split(',',$arr[$i]);

            my $hash = {};
            for my $j(0..$#keys){
                    $hash->{$keys[$j]} = $splitRec[$j]
            }
            push @hashArry,$hash;
    }
    return \@hashArry, \@keys;
}

3 Comments

Your link is broken. And there is nothing about XML in this code.
copy paste error, Added direct link, Edit didn't accept link with name
@sandeep you didn't get my question I guess, this is not what I asked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.