Loading ...
Sorry, an error occurred while loading the content.

Using data from a CSV file

Expand Messages
  • GoPi
    Hi folks, I am trying to read data from a csv file,basing on the data from one column I want to print the data from other column.I wrote the following piece of
    Message 1 of 4 , Feb 17, 2010
    • 0 Attachment
      Hi folks,

      I am trying to read data from a csv file,basing on the data from one column I want to print the data from other column.I wrote the following piece of code to accomplish this but nothing is getting printed.Where am I going wrong?Can someone pls help?

      #!/usr/local/bin/perl

      use strict;
      my $file;
      my $line;
      my @s = ();


      $file = 'dev.csv';

      open (F, $file) || die ("Could not open $file!");

      while ($line = <F>){
      chomp;
      my @s = split /,/;

      print $s[1]; /*Nothings printed here*/

      if ( $s[4] eq "CAN NOT STRIP"){
      print $s[1];

      }
      }

      close (F);
    • Kenneth Wolcott
      Hi; Parsing CSV files can be difficult. Even parsing well-formed, regular and simple CSV files can be tricky. There are Perl modules to do this for you.
      Message 2 of 4 , Feb 17, 2010
      • 0 Attachment
        Hi;

        Parsing CSV files can be difficult. Even parsing well-formed, regular and
        simple CSV files can be tricky.

        There are Perl modules to do this for you. Search CPAN at
        http://search.cpan.org/ for CSV.

        I suspect, however, that you would like to parse the CSV file yourself and
        that you are having problems with they way you are using the Perl split
        function.

        If you have a very simple CSV file that does not have any embedded commas
        inside the quotes and does not have any embedded quotes, you might be able
        to derive a regular expression that might work to parse your CSV file.

        Your current split function is way too simplistic for a typical CSV file.

        Ken Wolcott

        On Wed, Feb 17, 2010 at 18:02, GoPi <gopichand84@...> wrote:

        >
        >
        > Hi folks,
        >
        > I am trying to read data from a csv file,basing on the data from one column
        > I want to print the data from other column.I wrote the following piece of
        > code to accomplish this but nothing is getting printed.Where am I going
        > wrong?Can someone pls help?
        >
        > #!/usr/local/bin/perl
        >
        > use strict;
        > my $file;
        > my $line;
        > my @s = ();
        >
        > $file = 'dev.csv';
        >
        > open (F, $file) || die ("Could not open $file!");
        >
        > while ($line = <F>){
        > chomp;
        > my @s = split /,/;
        >
        > print $s[1]; /*Nothings printed here*/
        >
        > if ( $s[4] eq "CAN NOT STRIP"){
        > print $s[1];
        >
        > }
        > }
        >
        > close (F);
        >
        >
        >


        [Non-text portions of this message have been removed]
      • gopi chand
        Hi Ken, Thanks for the reply.I tried installing CSV modules through CPAN but it didnt work out for me...I wrote one more work which seems to be a better one
        Message 3 of 4 , Feb 17, 2010
        • 0 Attachment
          Hi Ken,

          Thanks for the reply.I tried installing CSV modules through CPAN but it didnt work out for me...I wrote one more work which seems to be a better one than the previous one but the following if condition fails in the program.Can someone pls help in making this if condition true

          if ($field4 =~ m/CAN NOT STRIP/) --->This condition fails


          #!/usr/local/bin/perl

          use strict;
          my $file;
          my $line;
          my $field1;
          my $field2;
          my $field3;
          my $field4;
          my $i;

          $file = 'dev.csv';

          open (F, $file) || die ("Could not open $file!");

          while ($line = <F>)
          {
          ($field1,$field2,$field3,$field4) = split ',', $line;


          print "$field4\n";
          print "$field1\n";



          }
          for ($i=1;$i<350;$i++)

          {
          print "inside1";
          if ($field4 =~ m/CAN NOT STRIP/)
          {
          print "inside";
          print "$field1\n";

          }
          }





          --- On Wed, 2/17/10, Kenneth Wolcott <kennethwolcott@...> wrote:

          > From: Kenneth Wolcott <kennethwolcott@...>
          > Subject: Re: [PBML] Using data from a CSV file
          > To: perl-beginner@yahoogroups.com
          > Date: Wednesday, February 17, 2010, 7:00 PM
          > Hi;
          >
          >   Parsing CSV files can be difficult.  Even
          > parsing well-formed, regular and
          > simple CSV files can be tricky.
          >
          >   There are Perl modules to do this for you. 
          > Search CPAN at
          > http://search.cpan.org/ for CSV.
          >
          >   I suspect, however, that you would like to parse the
          > CSV file yourself and
          > that you are having problems with they way you are using
          > the Perl split
          > function.
          >
          >   If you have a very simple CSV file that does not
          > have any embedded commas
          > inside the quotes and does not have any embedded quotes,
          > you might be able
          > to derive a regular expression that might work to parse
          > your CSV file.
          >
          >   Your current split function is way too simplistic
          > for a typical CSV file.
          >
          > Ken Wolcott
          >
          > On Wed, Feb 17, 2010 at 18:02, GoPi <gopichand84@...>
          > wrote:
          >
          > >
          > >
          > > Hi folks,
          > >
          > > I am trying to read data from a csv file,basing on the
          > data from one column
          > > I want to print the data from other column.I wrote the
          > following piece of
          > > code to accomplish this but nothing is getting
          > printed.Where am I going
          > > wrong?Can someone pls help?
          > >
          > > #!/usr/local/bin/perl
          > >
          > > use strict;
          > > my $file;
          > > my $line;
          > > my @s = ();
          > >
          > > $file = 'dev.csv';
          > >
          > > open (F, $file) || die ("Could not open $file!");
          > >
          > > while ($line = <F>){
          > > chomp;
          > > my @s = split /,/;
          > >
          > > print $s[1]; /*Nothings printed here*/
          > >
          > > if ( $s[4] eq "CAN NOT STRIP"){
          > > print $s[1];
          > >
          > > }
          > > }
          > >
          > > close (F);
          > >
          > > 
          > >
          >
          >
          > [Non-text portions of this message have been removed]
          >
          >
          >
          > ------------------------------------
          >
          > Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.htmlYahoo!
          > Groups Links
          >
          >
          >     perl-beginner-fullfeatured@yahoogroups.com
          >
          >
          >
        • Kenneth Wolcott
          Hi Gopi; I will insert comment(s) in your reply. ... It will help greatly the readability of your code if you will indent conditional/loop blocks. ... This can
          Message 4 of 4 , Feb 18, 2010
          • 0 Attachment
            Hi Gopi;

            I will insert comment(s) in your reply.

            On Wed, Feb 17, 2010 at 19:50, gopi chand <gopichand84@...> wrote:

            >
            >
            > Hi Ken,
            >
            > Thanks for the reply.I tried installing CSV modules through CPAN but it
            > didnt work out for me...I wrote one more work which seems to be a better one
            > than the previous one but the following if condition fails in the
            > program.Can someone pls help in making this if condition true
            >
            > if ($field4 =~ m/CAN NOT STRIP/) --->This condition fails
            >

            It will help greatly the readability of your code if you will indent
            conditional/loop blocks.


            >
            > #!/usr/local/bin/perl
            >
            > use strict;
            > my $file;
            > my $line;
            > my $field1;
            > my $field2;
            > my $field3;
            > my $field4;
            > my $i;
            >

            This can be rewritten for readability as:
            my ($file, $line, $field1, $field2, $field3, $field4, $i);

            However, when you start declaring variables like $field1, $field2 and
            $field3, you should instead do one of the following:

            If the positional fields have meaning, use a meaningful (mnemonic)
            variable name for each variable.

            If it is too much hassle to do that (ex: twenty fields?!), then you should
            use an array.

            Another idea is that the $line variable really is in the while block
            scope, so why not declare it so that it is localized to the while block
            scope like this:

            while (my $line = <F>) {
            # block of the while loop here
            }

            >
            >
            > $file = 'dev.csv';
            >
            > open (F, $file) || die ("Could not open $file!");
            >
            > while ($line = <F>)
            > {
            > ($field1,$field2,$field3,$field4) = split ',', $line;
            >
            > print "$field4\n";
            > print "$field1\n";
            >
            > }
            > for ($i=1;$i<350;$i++)
            >
            > {
            > print "inside1";
            > if ($field4 =~ m/CAN NOT STRIP/)
            > {
            > print "inside";
            > print "$field1\n";
            >
            > }
            >

            I'm wondering if you are not properly modeling the structure of your csv
            file in your script.

            You have only read one line so far.

            Why do you loop for 1 to 350? Do you have 350 columns or 350 rows?

            What is the while loop for versus the for loop?

            Ken Wolcott


            [Non-text portions of this message have been removed]
          Your message has been successfully submitted and would be delivered to recipients shortly.