File I/O: want to compare two or more lines @ once 
Author Message
 File I/O: want to compare two or more lines @ once

Hi there!

Hope some1 can help me: I am reading a file line by line. Every line
having the same format (so I can use "split" and an array in order to
lay hands on the variables). Now I want to compare the first line with
the second. How do I manage this? I am having no ideas right now!
Example:
a b 1
a b 2
a b 3
a c 4
a c 6
b a 7
b c 8
Above thats my file. I want to add all numbers if the firs two items of
two or more lines are equal, e.g.:
a b 1
a b 2
a b 3
Should return me a value of 1+2+3=6
Then
a c 4
a c 6
should be added to a value of 4+6=10.
And so on...
Hope anybody can help me out with that!



Fri, 28 Jan 2005 13:40:48 GMT  
 File I/O: want to compare two or more lines @ once
Also sprach Hemant Sharma:

Quote:
> Example:
> a b 1
> a b 2
> a b 3
> a c 4
> a c 6
> b a 7
> b c 8
> Above thats my file. I want to add all numbers if the firs two items of
> two or more lines are equal, e.g.:
> a b 1
> a b 2
> a b 3
> Should return me a value of 1+2+3=6
> Then
> a c 4
> a c 6
> should be added to a value of 4+6=10.

Use a hash for that:

    my %data;
    while (<DATA>) {


    }

    print $data{'a b'}, "\n";
    print $data{'a c'}, "\n";

    __DATA__
    a b 1
    a b 2
    a b 3
    a c 4
    a c 6

Tassilo
--

pam{rekcahbus;})(rekcah{lrePbus;})(lreP{rehtonabus;})(rehtona{tsuJbus!;
$_=reverse;s/sub/(reverse"bus").chr(32)/xge;tr~\n~~d;eval;



Fri, 28 Jan 2005 11:55:50 GMT  
 File I/O: want to compare two or more lines @ once

Quote:

> > Example:
> > a b 1
> > a b 2
> > a b 3
> > a c 4
> > a c 6
> > b a 7
> > b c 8
> > Above thats my file. I want to add all numbers if the firs two items of
> > two or more lines are equal, e.g.:
> > a b 1
> > a b 2
> > a b 3
> > Should return me a value of 1+2+3=6
> > Then
> > a c 4
> > a c 6
> > should be added to a value of 4+6=10.

> Use a hash for that:
>     my %data;
>     while (<DATA>) {


>     }
>     print $data{'a b'}, "\n";
>     print $data{'a c'}, "\n";

Ok but look:
I am not only having
a b
and
a c
Its not sure whats coming the next lines! it can be three times a b then
suddenly 5 times a c then maybe 3 times d e then 8 times u o or sumthin like
that!


Fri, 28 Jan 2005 14:23:14 GMT  
 File I/O: want to compare two or more lines @ once

Quote:

> > > Example:
> > > a b 1
> > > a b 2
> > > a b 3
> > > a c 4
> > > a c 6
> > > b a 7
> > > b c 8
> > > Above thats my file. I want to add all numbers if the firs two items of
> > > two or more lines are equal, e.g.:
> > > a b 1
> > > a b 2
> > > a b 3
> > > Should return me a value of 1+2+3=6
> > > Then
> > > a c 4
> > > a c 6
> > > should be added to a value of 4+6=10.

> > Use a hash for that:
> >     my %data;
> >     while (<DATA>) {


> >     }
> >     print $data{'a b'}, "\n";
> >     print $data{'a c'}, "\n";

> Ok but look:
> I am not only having
> a b
> and
> a c
> Its not sure whats coming the next lines! it can be three times a b then
> suddenly 5 times a c then maybe 3 times d e then 8 times u o or sumthin like
> that!

Have you even tried the code?  Your "sumthin like that" is no problem
for Tassilo's solution.  It just doesn't print everything it gathered,
but then you didn't specify what to do with the sums.

See "perldoc -f keys" for how to access everything in the hash.

Anno



Fri, 28 Jan 2005 12:38:14 GMT  
 File I/O: want to compare two or more lines @ once

Quote:

> Have you even tried the code?  Your "sumthin like that" is no problem
> for Tassilo's solution.  It just doesn't print everything it gathered,
> but then you didn't specify what to do with the sums.

> See "perldoc -f keys" for how to access everything in the hash.

> Anno

Yes tried the code and it is working. The sums are only to be printed out,
nothing more.


Fri, 28 Jan 2005 14:50:39 GMT  
 File I/O: want to compare two or more lines @ once
Hemant Sharma wrote at Mon, 12 Aug 2002 14:40:48 +0200:

Quote:
> Hi there!

> Hope some1 can help me: I am reading a file line by line. Every line
> having the same format (so I can use "split" and an array in order to
> lay hands on the variables). Now I want to compare the first line with
> the second. How do I manage this? I am having no ideas right now!
> Example:
> a b 1
> a b 2
> a b 3
> a c 4
> a c 6
> b a 7
> b c 8
> Above thats my file. I want to add all numbers if the firs two items of
> two or more lines are equal, e.g.:
> a b 1
> a b 2
> a b 3
> Should return me a value of 1+2+3=6
> Then
> a c 4
> a c 6
> should be added to a value of 4+6=10.

my $last_key = "";
my $current_sum = 0;
while (<DATA>) {
   chomp;
   my ($key, $value) = /(.*) (\d+)/;
   if ($last_key eq $key) {
      $current_sum += $value;
   } else {
      print $current_sum, "\n" if $last_key;
      $current_sum = $value;
      $last_key = $key;
   }

Quote:
}

__DATA__
a b 1
a b 2
a b 3
a c 4
a c 6
b a 7
b c 8

Greetings,
Janek



Fri, 28 Jan 2005 11:54:08 GMT  
 File I/O: want to compare two or more lines @ once

Quote:


>> Have you even tried the code?  Your "sumthin like that" is no problem
>> for Tassilo's solution.  It just doesn't print everything it gathered,
>> but then you didn't specify what to do with the sums.

>> See "perldoc -f keys" for how to access everything in the hash.

>> Anno

> Yes tried the code and it is working. The sums are only to be printed out,
> nothing more.

Anno just showed you how to find a way to do that. Did you run the
perldoc command?

Cheers,
Bernard
--
echo 42|perl -pe '$#="Just another Perl hacker,"'



Fri, 28 Jan 2005 13:00:44 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Comparing lines in two files using arrays.

2. To compare two files and search for a word in both files

3. Two Text files at once?

4. line by line comparison of two files

5. how can I read lines from file compare it and write it to another file

6. perl module to compare two files?

7. compare two files

8. compare two files - help!!

9. Help comparing two files using hashes

10. comparing the time stamp of two files

11. How to compare two files?

12. To compare the content in two files..

 

 
Powered by phpBB® Forum Software