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

Re: [PBML] Using data from a CSV file

Expand Messages
  • 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 1 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.