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

How to read specific information on a file

Expand Messages
  • nazanin
    Hi, I am very new to the Perl. And I have the following problem I have a file which has 1000s of variables: X1=0 Y1=1 B1=0 X2=1 …. X1000=1 I need to read
    Message 1 of 2 , Jun 8, 2010
    • 0 Attachment
      Hi,
      I am very new to the Perl. And I have the following problem
      I have a file which has 1000s of variables:
      X1=0
      Y1=1
      B1=0
      X2=1
      ….
      X1000=1
      I need to read this file and only collect the data which starts with X.
      I s this best way to do this:
      $x1 = uc('x1');
      $x2=uc(`x2');
      ….
      $x1000=uc(`x1000;);

      $fileOut = $file0 . '.txt' ;
      open (VIN, "$fileIn") ;
      open (VOUT, ">$fileOut") ;


      while ( $line_in = <VIN> )
      {
      $v1 = index ($line_in, $x1) ;
      $v2 = index ($line_in, $x1) ;
      ……
      $v1000 =….
      if ($v1 >= 0)
      {
      chomp($line_in) ;
      $p2 = index ($line_in, $x1) ;
      $p3 = $p2+length($x1) ;
      $p4 = length($line_in) ;

      $varName = substr($line_in, $p3, $p4 - $p3);

      printf VOUT "$x1 %d\n", $varName;

      And write this if statement for 1000 times with new variable as x2, x3 ,…
      Best Regards,
      Nazanin
    • Shlomi Fish
      ... No, it s not. See: http://perl.plover.com/varvarname.html You should use an array instead and instead of the index/substr/etc. menace, you should probably
      Message 2 of 2 , Jun 8, 2010
      • 0 Attachment
        On Tuesday 08 Jun 2010 21:01:17 nazanin wrote:
        > Hi,
        > I am very new to the Perl. And I have the following problem
        > I have a file which has 1000s of variables:
        > X1=0
        > Y1=1
        > B1=0
        > X2=1
        > ….
        > X1000=1
        > I need to read this file and only collect the data which starts with X.
        > I s this best way to do this:
        > $x1 = uc('x1');
        > $x2=uc(`x2');
        > ….
        > $x1000=uc(`x1000;);
        >
        > $fileOut = $file0 . '.txt' ;
        > open (VIN, "$fileIn") ;
        > open (VOUT, ">$fileOut") ;
        >
        >
        > while ( $line_in = <VIN> )
        > {
        > $v1 = index ($line_in, $x1) ;
        > $v2 = index ($line_in, $x1) ;
        > ……
        > $v1000 =….
        > if ($v1 >= 0)
        > {
        > chomp($line_in) ;
        > $p2 = index ($line_in, $x1) ;
        > $p3 = $p2+length($x1) ;
        > $p4 = length($line_in) ;
        >
        > $varName = substr($line_in, $p3, $p4 - $p3);
        >
        > printf VOUT "$x1 %d\n", $varName;
        >
        > And write this if statement for 1000 times with new variable as x2, x3 ,…

        No, it's not. See:

        http://perl.plover.com/varvarname.html

        You should use an array instead and instead of the index/substr/etc. menace,
        you should probably use a regular expression. Please learn more of Perl using
        http://perl-begin.org/ .

        Regards,

        Shlomi Fish

        --
        -----------------------------------------------------------------
        Shlomi Fish http://www.shlomifish.org/
        Optimising Code for Speed - http://shlom.in/optimise

        God considered inflicting XSLT as the tenth plague of Egypt, but then
        decided against it because he thought it would be too evil.

        Please reply to list if it's a mailing list post - http://shlom.in/reply .
      Your message has been successfully submitted and would be delivered to recipients shortly.