Trying to get to values from each line in a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jonathan184
    New Member
    • Nov 2006
    • 154

    Trying to get to values from each line in a file

    Hi I am trying to get to values on each line of a text file and write to another file
    I am still new to this I tried to writing the following below

    Just to let you know i am still a newbie.

    Code:
     #!/usr/bin/perl
    
    user warnings;
    use strict;
    use Fcntl; 
    
    $OD = "filehandle";
    $filepath = "myorders.txt";
    my @line = open(OD, $FilePath) or die "file could not be opened"; 
    
    $line  = <OD>;
          print $line;
    close OD;
    
    
    for $line (<OD>){
    
    $i = "1";
    $a = ($line);
    
    #This is how the line looks like####
    # Line 1 contains:#
    #  D300[U]02115964[/U]000080000200US      NXLS0000000148883200701102007010900000000000000[U]05[/U]                                - 00000000                                                                                20061345000000001USA     000000.05                NN        00000000 #
    # I am trying to get what is underlined#
    
    $info = 'substr($a 5 6 7 8 9 10 11 12)';
    
    print "$info";
    $quantity = 'substr($a 80 81)';
    
    print "$quantity"
    
    sysopen(HTML, 'myvalues.txt', O_RDWR|O_EXCL|O_CREAT, 0755);
    printf  HTML "$info"     "$quantity \n"
    close(HTML);
    $i++
    
    };
  • GunnarH
    New Member
    • Nov 2006
    • 83

    #2
    Hmm.. Suggested reading:

    perldoc perlintro

    perldoc -f open

    perldoc -f substr

    Comment

    • rickumali
      New Member
      • Dec 2006
      • 19

      #3
      I recommend pack/unpack, Perl's answer to "packed data streams" like the one you just described. Here's a little program to get the first part:

      Code:
      #!/bin/perl
      
      $sample_line = "D30002115964000080000200US";
      
      @ary = unpack "A4A8A*", $sample_line;
      
      print "$ary[0]\n";
      print "$ary[1]\n";
      print "$ary[2]\n";
      When you run this program, you'll get:

      Code:
      D300
      02115964
      000080000200US
      Do a perldoc -f pack, and a perldoc -f unpack. Better, get yourself the Perl Cookbook, which has a good recipe for understanding this function. Or Google around for a pack/unpack tutorial. Good luck!

      Comment

      • jonathan184
        New Member
        • Nov 2006
        • 154

        #4
        Excellent thanks guys the perl doc links you guys sent are great and the unpak and pack great way to start it off thank you.

        I appreciate it.

        Comment

        Working...