Professional Web Applications Themes

help on comparing lines in a text file - PERL Beginners

so i have a text file that looks like this 10/04/2004 UPL TZOO CME CRDN WIBC PETD SMF 10/11/2004 UPL TZOO CME WIBC PETD VNBC AMED anyway each line has 1 date field and 100 stock symbols and they are in order. I am trying to compare the different lines to see what has changed. so I open the file and put the <FH> into an array array=<FH>; but $array[0]; = line 1 and $array[1]; = line 2. i keep thinking that i need to break down the array into each line and match a symbol to an array so ...

  1. #1

    Default help on comparing lines in a text file

    so i have a text file that looks like this

    10/04/2004 UPL TZOO CME CRDN WIBC PETD SMF
    10/11/2004 UPL TZOO CME WIBC PETD VNBC AMED

    anyway each line has 1 date field and 100 stock symbols and they are
    in order. I am trying to compare the different lines to see what has
    changed.

    so I open the file and put the <FH> into an array
    array=<FH>;

    but $array[0]; = line 1 and $array[1]; = line 2.

    i keep thinking that i need to break down the array into each line and
    match a symbol to an array so that $array[0] = 10/04/2004 and
    $array[1] = UPL then compare it to the 2nd line somehow but don't
    think I am thinking this problem out correctly.

    can someone help with my logic?

    tnx in advance,
    alden
    Alden Guest

  2. #2

    Default Re: help on comparing lines in a text file

    Alden Meneses wrote: 

    Use the split() function to get the lines in an array each. See the
    FAQ ("perldoc -q intersection") for a tip how to compare them.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Guest

  3. #3

    Default Re: help on comparing lines in a text file

    On Mon, 11 Oct 2004 22:57:51 -0700, Alden Meneses wrote:
     

    What do you mean by changed? Does the fact that WIBC and PETD appear in
    both lines above, but in a different order consitute a change?
     
    Yes. You need to have look at the split command. Type perldoc -f split at
    the command line for more info. By default this will split the line on
    whitespace which can be stored in an array (as you wanted). Below is an
    example script. It assumes that the order in which the codes appear is
    important and that each line has the same number of codes.
    HTH
    Chris.

    #! /usr/bin/perl -w

    use strict;

    my ($file) = ARGV;

    open(FILE, $file) or die "Can't open file $file: $!\n";

    my prev = ();

    while (<FILE>) {

    if (prev) {
    my curr = split;

    my $num = scalar curr; # get total number of fields

    for (my $i = 1; $i < $num; ++$i) { # start at 1 to ignore date
    if ($curr[$i] ne $prev[$i]) {
    print "$curr[0]: $prev[$i] has changed to $curr[$i]\n";
    }
    }
    }
    prev = split;
    }
    close(FILE) or die "Can't close file $file: $!\n";

    Chris Guest

  4. #4

    Default Help creating a simple local gateway for testing HTTP::Request

    Hello all,

    I was wondering if someone could help me. I have a script which send an
    HTTP::Request

    I would like to be able to view what it is sending. I thought I could just
    create use HTTP::Daemon for this, but I'm not getting any response from it.
    I want to see the information from the initial request with headers.

    Here is what I have...

    Request:

    $req = HTTP::Request->new(POST =>
    'http://localhost:83/cgi-bin/server.cgi' );

    And Daemon:

    #!/usr/bin/perl

    use vars qw( $req $d $c );
    ( $req, $d, $c ) = ();


    use strict;
    use HTTP::Daemon;

    $d = HTTP::Daemon->new;
    while ( $c = $d->accept ) {
    $req = $c->get_request;
    printf $req
    }
    $c = undef; # don't forget to close the socket


    Thanks,
    Tom

    Thomas Guest

  5. #5

    Default RE: help on comparing lines in a text file

    Alden Meneses wrote: 
    From what you have provided, I a assuming the data comes in two line sets and you want to compare these two line sets and see what has been added and/or deleted from one run to the next.

    I would probably use a hash and add 1 to each element in line 1 or odds and add 2 for line 2 or evens. Then a quick check using:
    1: Deleted from prior run
    2: Added since last run
    3: same as last run.

    Rebuild for set or if you honly have one, then a moot quiestion.

    Wags ;) 



    ************************************************** *****
    This message contains information that is confidential
    and proprietary to FedEx Freight or its affiliates.
    It is intended only for the recipient named and for
    the express purpose(s) described therein.
    Any other use is prohibited.
    ************************************************** *****

    Wagner, Guest

  6. #6

    Default Re: help on comparing lines in a text file

    I did something like you want, and maybe this could help you !!!!!!!. I copy my code.

    foreach my $linea1 ( <ARRAY1> )
    {
    my $match = 0;
    my comp = split /\|/, $linea1;
    foreach my $linea2 ( <ARRAY2> )
    {
    my comp2 = split /\|/, $linea2; # I use the pipe in my file but you can put a white space.
    if ( $comp[2] == $comp2[0] ){ $match=1; } # You have to put the lines you want to compare in both files.
    }
    if ( $match == 1 ){ print WRITE join("|", comp[2,3,0], "\n");}
    }

    Regards
    ----- Original Message -----
    From: Alden Meneses <com>
    To: org
    Subject: help on comparing lines in a text file
    Date: Mon, 11 Oct 2004 22:57:51 -0700
     

    --
    _______________________________________________
    Get your free email from http://mymail.bsdmail.com

    Powered by Outblaze
    Rafael Guest

  7. #7

    Default Re: help on comparing lines in a text file

    Thanks Gunnar,

    Thanks for pointing the split() function out. I get all 101 elements
    on each line now instead of the whole line being one element. I am
    currently trying to figure out how get the each line on its own array
    so that I can compare array1 vs. array2. Not sure how to do that in a

    while(<FH>){
    array1=split(/ /,$_);
    }

    i also found this bit of code in the perl cookbook that will help me
    find the differences with array2 vs. array1

    my %seen; # lookup table
    my array2;# answer

    # build lookup table
    seen{array1} = ();

    foreach $item (array2) {
    push(array2only, $item) unless exists $seen{$item};
    }

    i guess what i am also trying to do is a stock has moved up or down the array.

    On Tue, 12 Oct 2004 10:37:19 +0200, Gunnar Hjalmarsson
    <cc> wrote: 
    >
    > Use the split() function to get the lines in an array each. See the
    > FAQ ("perldoc -q intersection") for a tip how to compare them.
    >
    > --
    > Gunnar Hjalmarsson
    > Email: http://www.gunnar.cc/cgi-bin/contact.pl
    >
    > --
    > To unsubscribe, e-mail: org
    > For additional commands, e-mail: org
    > <http://learn.perl.org/> <http://learn.perl.org/first-response>
    >
    >[/ref]
    Alden Guest

Similar Threads

  1. Comparing Text String(s) to Inputed Text
    By Uneekness in forum Macromedia Flash Data Integration
    Replies: 0
    Last Post: July 5th, 04:16 PM
  2. Broken lines and text when creating an EPS file
    By Carlos_Cavazos@adobeforums.com in forum Adobe Indesign Macintosh
    Replies: 3
    Last Post: December 6th, 04:21 PM
  3. read lines of file without parsing the lines
    By W. Paulisse in forum PHP Development
    Replies: 3
    Last Post: February 11th, 01:25 AM
  4. Need help comparing lines in two files
    By Stuart Clemons in forum PERL Beginners
    Replies: 7
    Last Post: January 23rd, 01:24 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not 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