#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    11
    Rep Power
    0

    Saving data into a CSV file


    Hi,

    I have some data which is tab separated and I want it to be save directly to a csv format rather than saving into txt or dat.
    Please respond soon!

    Thanks in advance.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    828
    Rep Power
    496
    Do you mean that you want to replace tabs by comma in your data?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    11
    Rep Power
    0
    No, I found the solution for my query.

    The code is below!

    open (FILE, ">comma.csv");
    print (FILE "a,b,c,d,\n");
    print (FILE "f,g,h,i,\n");
    print (FILE "j,k,l,m,\n");
    close FILE;
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    828
    Rep Power
    496
    If that is all you wanted to do, you could have grabbed any decent tutorial on the Web, rather than crossposting your question on several forums. You would have learnt more this way.
  8. #5
  9. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,252
    Rep Power
    1810
    Seriously, if that's the solution, then the question needs to be: "How do I print three fixed strings into a file?"

    For anyone else, you'd be much better served using Text::CSV which has already been written to handle special conditions such as fields containing newline characters or commas.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    11
    Rep Power
    0
    Originally Posted by Laurent_R
    If that is all you wanted to do, you could have grabbed any decent tutorial on the Web, rather than crossposting your question on several forums. You would have learnt more this way.
    What else to do more than that certainly!
    I need to write my values into the csv file that's all. Moreover in my organisation there is a mess if you use modules which don't work like the Text::CSV which shows error!
    \
  12. #7
  13. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,252
    Rep Power
    1810
    Text::CSV is a working module. If you have difficulty using it, post an example here.

    Edit: The primary reason your solution isn't a good one, is that data is normally variable. That's usually the point of developing a program: to perform a set of actions on a changing, or unknown data set.

    If there are just three fixed lines you want added to a file, most users could get by with copy and paste.
    Last edited by keath; May 7th, 2013 at 08:36 AM.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    11
    Rep Power
    0
    Originally Posted by keath
    Text::CSV is a working module. If you have difficulty using it, post an example here.

    Edit: The primary reason your solution isn't a good one, is that data is normally variable. That's usually the point of developing a program: to perform a set of actions on a changing, or unknown data set.

    If there are just three fixed lines you want added to a file, most users could get by with copy and paste.
    I had compliance issue to paste my real code to the forum so why I have put an instance of that issue.
    I have spoken superiors and though have not much security problems now.
    The below code is a part of the process wherein I am trying to write the the values found in fail condition to an output file. That is what I am working upon.

    ## Writes the begining time of the process to output file
    open (OUT, ">>$out") || die "Couldn't create the output file to write output data.\n";
    print (OUT "Process start time: $mytime\n");

    ## Searches for the required pattern in the .csv file
    open (FILE, "<$infile") || die "Input processing file is missing.\n";
    @infile = <FILE>;

    ## Displays the user the begining of the process
    printf "\n\t=*=*=*==*= PROCESS STARTED =*=*=*==*=\t\n\n";

    ## Display the 1st lines of output as below
    print (OUT "Type,DATE,TIME,PARAMETER_NAME,\n");

    foreach $val (@infile)
    {
    chomp ($val);

    ### TAkes header names into a string
    if ($val =~ /^Date/)
    {
    @header = split (",",$val);
    }
    next if $val =~ /^Date/;

    ### Reads each line and stores into the array
    @lines = split (",", $val);

    ### Undergoes each pass criteria to be checked to attain the fail condition
    for ($i = 0; $i<=$#header; $i++)
    {
    ##### Type-1 Criteria #####
    for ($a = 1; $a <=$#header; $a++)
    {
    $data = "$pass1[$a]";
    if ($data eq "$header[$i]")
    {
    if ($lines[$i] eq "0")
    {
    print (OUT "Type-1,$lines[0],$lines[1],$header[$i],\n");
    }
    }
    }
    ##### Type-2 Criteria #####
    for ($a = 1; $a <=$#pass2; $a++)
    {
    #printf "$pass2[$a]\n";
    $data = "$pass2[$a]";
    if ($data eq "$header[$i]")
    {
    if ($lines[$i] ne "1")
    {
    print (OUT "Type-2,$lines[0],$lines[1],$header[$i],\n");
    }
    }
    }
    ##### Type-3 Criteria #####
    for ($a = 1; $a <=$#header; $a++)
    {
    #printf "$pass3[$a]\n";
    $data = "$pass3[$a]";
    if ($data eq "$header[$i]")
    {
    #$passVal3 = 0;
    if ($lines[$i] ne "0")
    {
    #printf "$lines[$i] - $passVal3\n";
    print (OUT "Type-3,$lines[0],$lines[1],$header[$i],\n");
    }
    }
    }
    ##### Type-4 Criteria #####
    for ($a = 1; $a <=$#pass4; $a++)
    {
    $data = "$pass4[$a]";
    if ($data eq "$header[$i]")
    {
    $passVal4 = "";
    if ($lines[$i] ne "0")
    {
    print (OUT "Type-4,$lines[0],$lines[1],$header[$i],\n");
    }
    }
    }
    ##### Type-5 Criteria #####
    for ($a = 1; $a <=$#pass5; $a++)
    {
    $data = "$pass5[$a]";
    if ($data eq "$header[$i]")
    {
    #$passVal5 = 0;
    if ($lines[$i] ne "0")
    {
    print (OUT "Type-5,$lines[0],$lines[1],$header[$i],\n");
    }
    }
    }
    }
    }

    ## Closes all the files used
    close (DATA);
    close (OUT);
    close (FILE);

    ## Displays the user the completion of the process
    printf "\n\n\t=*=*=*==*= PROCESS COMPLETED =*=*=*==*=\t\n\n";

    Suggest anything needs more to be encased here.

IMN logo majestic logo threadwatch logo seochat tools logo