# comparing two numbers

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

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

usaims

## Re: comparing two numbers

> 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

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

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

## Re: comparing two numbers

>
>> 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 = ();

# 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";
}

## Re: comparing two numbers

David K. Wall wrote:

> 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 :-(

--
John                   Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available:     http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

## Re: comparing two numbers

> David K. Wall wrote:
>
>> 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 :-(

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

## Re: comparing two numbers

>
> > David K. Wall wrote:
> >
> >> 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 :-(
>
> 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

## Re: comparing two numbers

Anno Siegel wrote:

>>
>> > David K. Wall wrote:
>> >
>> >> 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 :-(
>>
>> 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.

Read better next time, all the clues are there.

--
John                   Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available:     http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

## Re: comparing two numbers

> Anno Siegel wrote:
>
> >>
> >> > David K. Wall wrote:
> >> >
> >> >> 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 :-(
> >>
> >> 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.
>
> 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

## Re: comparing two numbers

anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote:

> comp.lang.perl.misc:
>>
>> > David K. Wall wrote:
>> >
>> >> 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 :-(
>>
>> 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.

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>

## Re: comparing two numbers

> anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote:
> > comp.lang.perl.misc:
> >> > David K. Wall wrote:
> >> >
> >> >> 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 :-(
> >>
> >> 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.
>
> 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

## Re: comparing two numbers

Anno Siegel wrote:

>> anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote:
>> > comp.lang.perl.misc:
>> >> > David K. Wall wrote:
>> >> >
>> >> >> 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 :-(
>> >>
>> >> 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.
>>
>> 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.

:-D or maybe :-(.

--
John                   Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available:     http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

## Re: comparing two numbers

A. Sinan Unur wrote:
>
> > 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
>
> 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
>
> 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.

## Re: comparing two numbers

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

## Re: comparing two numbers

perlmodule@gmail.com wrote:

> 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--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org