CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Obtain beg/end numbers from previous lines

 



yoda
New User

Jul 28, 2010, 2:48 PM

Post #1 of 4 (524 views)
Obtain beg/end numbers from previous lines Can't Post

I'm very new to Perl and need help with a script I'm trying to create. I don't have a programming background, so please bear with me. I'm entering a sample of my original file and then a sample of the desired output below. The original file will be at least tens of thousands of lines, so not sure of the best way to read in the file.

The "Y" character signals the StartNumber, and then the EndNumber will be the last id number before the next start. It's possible a start/end number will be the same, as in the example on the first line.

Original file:
ID0001,TEMP,\\DATA\Folder,Y,,,
ID0002,TEMP,\\DATA\Folder,Y,,,
ID0003,TEMP,\\DATA\Folder,,,,
ID0004,TEMP,\\DATA\Folder,Y,,,
ID0005,TEMP,\\DATA\Folder,,,,
ID0006,TEMP,\\DATA\Folder,,,,
ID0007,TEMP,\\DATA\Folder,,,,
ID0008,TEMP,\\DATA\Folder,Y,,,
ID0009,TEMP,\\DATA\Folder,,,,
ID0010,TEMP,\\DATA\Folder,Y,,,

Desired Output:
StartNumber,Endnumber
ID0001,ID0001
ID0002,ID0003
ID0004,ID0007
ID0008,ID0009
ID0010,ID0010

This is the script I have come up with so far, but it only obtains the start number.

Code
open (TEXT, 'Input.txt'); 
while (<TEXT>) {
chomp;
($pageid, $vol, $path, $docyes, $one, $two, $three) = split(",");


if ($docyes eq "Y") {
$Begnum = $pageid;
print "$Begnum,\n"

}

next;

}
close (FILE);
exit;


Any help appreciated. Thanks


BillKSmith
Veteran

Jul 29, 2010, 6:57 AM

Post #2 of 4 (495 views)
Re: [yoda] Obtain beg/end numbers from previous lines [In reply to] Can't Post

Notes on your code:

  • close refers to the wrong handle.


  • open should always check for errors.


  • The three argument form of open would be much better.


  • Always use "use strict" and "use warnings". You do have to declare your variables under strict.


  • chomp is unnecessary unless you plan on using $three.


  • next is unnecessary at end of while loop


  • Your parsing is fine. If speed really is an issue and real data has a fixed format like your sample, experiment with unpack.


  • I like to think of the print as lagging one line behind the read. This requires one print after the loop to print the last entry.


    Code
    use strict; 
    use warnings;
    my $Begnum;
    my $Endnum;
    *TEXT = *DATA;
    #open (TEXT, '<', 'Input.txt') or die "Cannot open Input.txt";
    while (<TEXT>) {
    chomp;
    my ($pageid, $vol, $path, $docyes, $one, $two, $three) = split(/,/);
    if ($docyes eq "Y") {
    print "$Begnum,$Endnum\n" if defined $Endnum;
    $Begnum = $pageid;
    }
    $Endnum = $pageid;

    }
    close (TEXT);
    print "$Begnum,$Endnum\n";
    exit;
    __END__
    ID0001,TEMP,\\DATA\Folder,Y,,,
    ID0002,TEMP,\\DATA\Folder,Y,,,
    ID0003,TEMP,\\DATA\Folder,,,,
    ID0004,TEMP,\\DATA\Folder,Y,,,
    ID0005,TEMP,\\DATA\Folder,,,,
    ID0006,TEMP,\\DATA\Folder,,,,
    ID0007,TEMP,\\DATA\Folder,,,,
    ID0008,TEMP,\\DATA\Folder,Y,,,
    ID0009,TEMP,\\DATA\Folder,,,,
    ID0010,TEMP,\\DATA\Folder,Y,,,

    Good Luck,
    Bill


    yoda
    New User

    Jul 30, 2010, 7:53 AM

    Post #3 of 4 (482 views)
    Re: [BillKSmith] Obtain beg/end numbers from previous lines [In reply to] Can't Post

    Thank you Bill. I received the below warning, but otherwise it seems to work great (also had to uncomment out the # sign).

    Warning received
    Name "main::DATA" used only once: possible type at ... line 5


    BillKSmith
    Veteran

    Jul 30, 2010, 12:14 PM

    Post #4 of 4 (476 views)
    Re: [yoda] Obtain beg/end numbers from previous lines [In reply to] Can't Post

    I added line 5 and removed line 6 to make it read TEXT from the data at the end of the file. You should remove line 5 and replace line 6 to use your own data file. (Removing line 5 will eliminate the warning.)
    Good Luck,
    Bill

     
     


    Search for (options) Powered by Gossamer Forum v.1.2.0

    Web Applications & Managed Hosting Powered by Gossamer Threads
    Visit our Mailing List Archives