comparing two numbers

U

usaims

Hi, I'm new to Perl and I have limited programming skills. I'm trying
to see if numbers in a list are not identical, for example:

1207553365
1207553365
1207553365
1207553365
1207553365
1207553365
1207553365
1207553366

Noticed that the last one is different, I want perl to tell me that. A
little kick in the butt would be greatly appreciated. Thanks in
advance.

usaims
 
A

A. Sinan Unur

Hi, I'm new to Perl and I have limited programming skills. I'm trying
to see if numbers in a list are not identical, for example:

1207553365
1207553365
1207553365
1207553365
1207553365
1207553365
1207553365
1207553366

Noticed that the last one is different, I want perl to tell me that. A
little kick in the butt would be greatly appreciated. Thanks in
advance.

Then you need to specify the problem a little better. For example, given
the input below, what output would you like your program to produce?

Please do read the posting guidelines to find out how you can help others
help you.

You will need to better describe what you are trying to do and make an
effort yourself if you are to get the maximum possible help here.

Here is something that might do what you are after. Please study it (not
as a great example of programming) to try and understand in what ways it
meets your specs, and in what ways it doesn't, and then formulate your
next attempt based on that new understanding.

#! /usr/bin/perl

use strict;
use warnings;

if(defined(my $prev = <DATA>)) {
chomp($prev);
while(defined(my $curr = <DATA>)) {
chomp $curr;
last unless length $curr;
unless($curr == $prev) {
print "$.: $curr not equal to $prev\n";
$prev = $curr;
}
}
}

__END__
1207553365
1207553365
1207553365
1207553365
1207553365
1207553365
1207553365
1207553366
1207553365

Sinan
 
D

David K. Wall

A. Sinan Unur said:
Hi, I'm new to Perl and I have limited programming skills. I'm
trying to see if numbers in a list are not identical, for
example:
[snip]
You will need to better describe what you are trying to do and
make an effort yourself if you are to get the maximum possible
help here.

Agreed. But I still might recommend looking at
'perldoc -q duplicate'.
Here is something that might do what you are after. Please study
it (not as a great example of programming) to try and understand
in what ways it meets your specs, and in what ways it doesn't, and
then formulate your next attempt based on that new understanding.

Here's another approach, which is what happens when the problem is
not specified well:

use strict;
use warnings;

my @nums = qw(
1207553365
1207553365
1207553365
1207553365
1207553365
1207553365
1207553365
1207553366
1207553365
);

my %hash;
@hash{@nums} = ();

# a little long-winded here, but I thought using ?:
# might confuse the OP

if ( 1 == keys %hash ) {
print "All the same\n";
}
else {
print "At least one is different\n";
}
 
J

John Bokma

David said:
if ( 1 == keys %hash ) {
print "All the same\n";

Aargh, keys %hash = 1 with warnings on gives:
Found = in conditional, should be == at ...

Never understood crippled^H^H^H^H^H^H^H^Hdefensive programming :-(
 
D

David K. Wall

John Bokma said:
Aargh, keys %hash = 1 with warnings on gives:
Found = in conditional, should be == at ...

Never understood crippled^H^H^H^H^H^H^H^Hdefensive programming :-(

It wasn't intended to be defensive. I typed it that way because I was
too lazy to type an extra set of parentheses. :)
 
A

Anno Siegel

David K. Wall said:
It wasn't intended to be defensive. I typed it that way because I was
too lazy to type an extra set of parentheses. :)

But you don't have to. "keys" binds tightly enough for "keys %hash == 1"
to work. The warning about "=" instead of "==" is unrelated.

Anno
 
J

John Bokma

Anno said:
But you don't have to. "keys" binds tightly enough for "keys %hash == 1"
to work. The warning about "=" instead of "==" is unrelated.

Read better next time, all the clues are there.
 
A

Anno Siegel

John Bokma said:
Read better next time, all the clues are there.

There is no need to revise my reading. I am addressing David's actual
intent (avoiding parentheses) in putting "1" first, not the defensive
one (provoking a syntax error in case of "=") you imputed.

Anno
 
D

David Wall

But you don't have to. "keys" binds tightly enough for "keys
%hash == 1" to work. The warning about "=" instead of "==" is
unrelated.

I trust you to be correct, but if I'm too lazy to type a set of
parentheses, what makes you think I remember or will check to see
how tightly keys() binds? <smile>
 
A

Anno Siegel

David Wall said:
I trust you to be correct, but if I'm too lazy to type a set of
parentheses, what makes you think I remember or will check to see
how tightly keys() binds? <smile>

Ah, but then John was right in seeing it as defensive programing.

Anno
 
U

usaims

A. Sinan Unur said:
Then you need to specify the problem a little better. For example, given
the input below, what output would you like your program to produce?

I would like the program to tell me which number is different. Each
number represents a unique number to a physical server.
Please do read the posting guidelines to find out how you can help others
help you.

You will need to better describe what you are trying to do and make an
effort yourself if you are to get the maximum possible help here.

Basically, the numbers that I produced below where parsed from a log
file.

EXAMPLE:
<Endpoint ep='10.50.19.77:9876'>
<SoapVersion>1207553365</SoapVersion>
<Status>ok</Status>
</Endpoint>
</Control>
<Control>
<Endpoint ep='10.15.19.78:9876'>
<SoapVersion>1207553365</SoapVersion>
<Status>ok</Status>
</Endpoint>
</Control>
<Control>
<Endpoint ep='10.15.19.79:9876'>
<SoapVersion>1207553365</SoapVersion>
<Status>ok</Status>
</Endpoint>

This is the code I used to parse it:

my $start_tag = '<SoapVersion>';
my $end_tag = '</SoapVersion>';

open(FILE, '/tmp/test.whatever') || die "can't open the file: $!";
my $line = <FILE>;
while(defined ( $line = <FILE> ) ) {

if ( $line =~ m/$start_tag(.+?)$end_tag/g )


99% of the time all these numbers are identical, when one is different
from the other, I want Perl to tell me that.
Here is something that might do what you are after. Please study it (not
as a great example of programming) to try and understand in what ways it
meets your specs, and in what ways it doesn't, and then formulate your
next attempt based on that new understanding.

I'm in the process of studying your example, thank you.
#! /usr/bin/perl

use strict;
use warnings;

if(defined(my $prev = <DATA>)) {
chomp($prev);
while(defined(my $curr = <DATA>)) {
chomp $curr;
Can you explain to me what 'last unless length $curr' ????
last unless length $curr;
unless($curr == $prev) {
print "$.: $curr not equal to $prev\n";
$prev = $curr;
}
}
}

__END__
1207553365
1207553365
1207553365
1207553365
1207553365
1207553365
1207553365
1207553366
1207553365

Sinan

I want to learn from you guys, thanks in advance.
 
S

Sherm Pendley

It will exit the while() loop if $curr has a length equal to 0.

What will?? What while() loop? Where is $curr?

Context please - quote the parts of the message you're replying to.

sherm--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top