Script to parse files

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Script to parse files

    I've been working with this since wolf and jeff and john sent me some stuff,
    I think I actually based everything on wolf's code excerpts. I'm sure my
    final code is going to not be perfect, but at least I have the piece of mind
    of knowing that I can get this thing some better then it was. I'm still not
    sure on making it a sub that I can use on anything, but I'll deal with that
    issue another day.

    Robert
    Lonewolf Guest

  2. Similar Questions and Discussions

    1. Make this into a script to parse?
      I'm back to dealing with the main issue of a badly formatted file being brought down from an archaic system and needing to be cleaned up before...
    2. Unable to parse and process php files
      Hello, Just installed Apache and PHP. Edited the .conf file for the appropariate addtype lines, and Apache itself is working. However, when I...
    3. How to parse large script faster
      I've created a perl script automatically based on an ini file (we want to replace the ini file holding a number of rules by regular expressions in...
    4. Library to parse mail files
      Hi, What would be that best library/module for parsing a mail file. I want to be able to extract the values for header tags as well as the...
    5. How to use .htaccess to parse only .html files as .php?
      ***newbie question*** Hi, I am trying to make my server (Apache) parse .html files as .php. I found this line of code: ForceType...
  3. #2

    Default Re: Script to parse files

    For Quality purpouses, LoneWolf 's mail on Friday 06 February 2004 16:57 may
    have been monitored or recorded as:
    > I've been working with this since wolf and jeff and john sent me some
    > stuff, I think I actually based everything on wolf's code excerpts. I'm
    > sure my final code is going to not be perfect, but at least I have the
    > piece of mind of knowing that I can get this thing some better then it was.
    > I'm still not sure on making it a sub that I can use on anything, but I'll
    > deal with that issue another day.
    easy: (notice: thats the same script as priviously but has the parse in a
    sub:)

    ---snip---
    #!/usr/bin/perl

    use strict;
    use warnings;
    my (@fields, $lng);

    sub whatever {

    if (@_) {

    foreach my $infile (@_) {

    * my ($i,$rec);

    * open INFILE, "$infile" or die "Can't open $infile: $!";
    * open OUTFILE, "${infile}.out" or die "Can't open ${infile}.out at home:
    $!";
    * while (<INFILE>) {
    * *$rec++;
    * *chomp;
    * *@fields = split /\s*\|\s*/, $_;
    * *$fields[0] =~ s/^\s+//;
    * *#there is probably a way to get rid of the trailing spaces in the first
    entry using split,I just couldnt think of any.

    * *$lng = @fields unless $lng; #set $lng for first record
    * *print "The following record: $i has ", scalar @fields, " fields as compared
    to $lng fields in the first record! Skip. : $_\n" and next unless $lng ==
    @fields;
    #poor quality control of your input data: check if all reords have the same
    number of fields or skip and print record otherwise.
    * *$i++;
    * *print OUTFILE $i;
    * *print OUTFILE "|$_" foreach (@fields);
    * *print OUTFILE "|$fields[0]\n"; #your trailing ID
    * }
    * close INFILE;
    * close OUTFILE;
    * print "Read $rec records from ./sql/$infile and printed $i into ./
    ${infile}.out\n";
    } #end foreach
    return 1;
    }
    else {return undef;}
    } #end sub whatever

    ---snap---


    call it with &whatever('path/to/firstfile', 'path/to/secondfile',...)

    Enjoy, Wolf

    Wolf Blaum Guest

  4. #3

    Default Re: Script to parse files

    For Quality purpouses, wolf blaum 's mail on Friday 06 February 2004 20:15 may
    have been monitored or recorded as:
    > easy: (notice: thats the same script as priviously but has the parse in a
    > sub:)
    >
    > ---snip---
    > #!/usr/bin/perl
    >
    > use strict;
    > use warnings;
    > my (@fields, $lng);

    .... and that my (@fields, $lng); belings in the sub....
    wolf

    Wolf Blaum Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139