lazy has asked for the wisdom of the Perl Monks concerning the following question:

i'm reading a header line(column name) from a file with the below code. i tried it with split() and returning values i need to store it in an array but its getting stored as single value in array ! can i use loop to store values in arrray or while reading itself i can do that ? needed help in this...

open (MYFILE, 'Data.txt'); $ColumnNames = <MYFILE>; @colarray = split ',', $ColumnNames;

this is what i read

Id,Name,Sex,RegDate,LeaveDate,Time,Dummy

this is the desired output which i should print it on file using Tie::File

Id,Name,Sex,RegDate,LeaveDate,Time

Replies are listed 'Best First'.
Re: Reading line and Spliting it
by Corion (Patriarch) on Jul 17, 2014 at 07:06 UTC

    Let me first recommend Text::CSV_XS or Spreadsheet::Read to you. Especially Spreadsheet::Read handles reading various tabular files, not only a limited subset of CSV files.

    You don't show us what data is in $ColumnNames so it's hard to say what might go wrong in your case. If your data is delimited with commas, using split would separate them into elements in @colarray.

    Maybe you can show us the relevant input and the output you get, and also tell us the output you expect so we can help you better?

      without those modules how to read column names only from a comma separated file

      Id,Name,Sex,RegDate,LeaveDate,Time,Dummy

      i need to read it but print/write like this

      Id,Name,Sex,RegDate,LeaveDate,Time

        So, where is your problem exactly? What is the code you have? What does your code output when run? What should it output instead?

        Just doing a split like this will lead to insanity at some point, unless you can completely insure your data NEVER has commas, or has COMPLETELY PREDICTABLE commas in it. The modules help cope with encapsulated data (When dumping to CSV, Excel will often encapsulate data with a comma in double quotes) and other potential stupidity so you don't $self->shoot('foot'). They aren't perfect, but they're a vast improvement over blindly splitting on an arbitrary delimiter for real world data.
Re: Reading line and Spliting it
by gurpreetsingh13 (Scribe) on Jul 17, 2014 at 09:55 UTC
    All well. Just get array values into scalars like:
    my ($Id,$Name,$Sex,$RegDate,$LeaveDate,$Time)=@colarray
    Remaining fields would be automatically ignored looking at Left Hand Side.

      it will be possible when the number of column names are fixed but in dynamic situation the number of columns may vary as i have to read many files like this !

Re: Reading line and Spliting it
by bulrush (Scribe) on Jul 17, 2014 at 10:51 UTC
    Whenever I read a file I read the complete file into an array so I can loop through the whole file (via the array). When I use split, someone recommended I use //, like this:
    @file = <MYFILE>;
    foreach $l (@file)
        {
        @colarray = split /,/, $l;
        }
    
    The biggest file I have read this way was 500,000 lines, with no problems.
    Perl 5.8.8 on Redhat Linux RHEL 5.5.56 (64-bit)

      You may want to investigate Tie::File as a way to simplify processing a file as an array.

      1 Peter 4:10