Help with Perl Q

D

Deepu

Hi All,

I have a problem and would like to get some ideas on the
implementation.

I have a file with contents as:

1.18 -> temp1
1.18 -> temp2
1.26 -> temp3
1.34 -> temp4
1.40 -> temp5
1.50 -> temp6
1.60 -> temp7
1.60 -> temp8
1.60 -> temp9

I need to get the output as:

temp9 -> temp1 => 0.42
temp9 -> temp2 => 0.42

temp8 -> temp1 => 0.42
temp8 -> temp2 => 0.42

temp7 -> temp1 => 0.42
temp7 -> temp2 => 0.42


Thanks for your time...
 
J

Jürgen Exner

Deepu said:
Hi All,

I have a problem and would like to get some ideas on the
implementation.

I have a file with contents as:

1.18 -> temp1
1.18 -> temp2
1.26 -> temp3
1.34 -> temp4
1.40 -> temp5
1.50 -> temp6
1.60 -> temp7
1.60 -> temp8
1.60 -> temp9

I need to get the output as:

temp9 -> temp1 => 0.42
temp9 -> temp2 => 0.42

temp8 -> temp1 => 0.42
temp8 -> temp2 => 0.42

temp7 -> temp1 => 0.42
temp7 -> temp2 => 0.42

Trivial:

use warnings; use strict;
for my $i (9, 8, 7) {
for my $j (1, 2) {
print "temp$ i-> temp$j => 0.42\n";
}
print "\n";
}

And you don't even need the input from that file.

If you meant to ask something different then maybe you want to explain
how e.g.
1.60 -> temp8
is related to temp8 or temp8->temp2 or 0.42 or anything at all in your
desired output.

jue
 
D

Deepu

If you meant to ask something different then maybe you want to explain
how e.g.
        1.60 -> temp8
is related to temp8 or temp8->temp2 or 0.42 or anything at all in your
desired output.

Sorry for my incomplete explanation..basically the file is an output
of some calculation and then i need to get the difference from each of
the largest value with each of the smallest value in the file.

For example:
* temp7, temp8 & temp9 are largest value in the file & temp1 & temp2
are smallest values in the file.
* Next i need to get the difference & print out as below.
* File has multiple largest & smallest numbers.

temp7 - temp1 -> 0.42
temp7 - temp2 -> 0.42

Thanks..
 
R

Ralph Malph

Please put the subject of your article in the Subject of your article.
I guess auto-posting your horseshit "posting guidelines" does a lot of
good!
ha ha ha
0\/\/ned!
 
C

ccc31807

Hi All,

I have a problem and would like to get some ideas on the
implementation.

Script and output below. This is a crude, inelegant solution, but it
may be all you need. The key is using parallel arrays to calculate and
print your results. The only trick is to build the parallel arrays,
and that took me a minute or two.

SCRIPT:
#! perl
use strict;
use warnings;

my %temps;
my (@lowest, @highest);
my $lowest = 1000000;
my $highest = -1000000;
while (<DATA>)
{
next unless /\d/;
chomp;
my ($temp, $name) = split(/ -> /, $_);
$lowest = $temp if $temp < $lowest;
$highest = $temp if $temp > $highest;
$temps{$name} = $temp;
}

foreach my $k (sort keys %temps)
{
push (@lowest, "$k:$temps{$k}") if $temps{$k} == $lowest;
unshift (@highest, "$k:$temps{$k}") if $temps{$k} == $highest;
}

#print "lowest: $lowest [@lowest], highest: $highest [@highest]\n";

foreach my $high (@highest)
{
my ($name1, $temp1) = split(/:/, $high);
foreach my $low (@lowest)
{
my ($name2, $temp2) = split(/:/, $low);
printf("%s minus %s is %f\n", $name1, $name2, $temp1 -
$temp2);
}
print "\n";
}

exit(0);

__DATA__
1.18 -> temp1
1.18 -> temp2
1.26 -> temp3
1.34 -> temp4
1.40 -> temp5
1.50 -> temp6
1.60 -> temp7
1.60 -> temp8
1.60 -> temp9

OUTPUT:
perl perlQ.plx
temp9 minus temp1 is 0.420000
temp9 minus temp2 is 0.420000

temp8 minus temp1 is 0.420000
temp8 minus temp2 is 0.420000

temp7 minus temp1 is 0.420000
temp7 minus temp2 is 0.420000
 
D

Deepu

I have a problem and would like to get some ideas on the
implementation.

Script and output below. This is a crude, inelegant solution, but it
may be all you need. The key is using parallel arrays to calculate and
print your results. The only trick is to build the parallel arrays,
and that took me a minute or two.

SCRIPT:
#! perl
use strict;
use warnings;

my %temps;
my (@lowest, @highest);
my $lowest = 1000000;
my $highest = -1000000;
while (<DATA>)
{
    next unless /\d/;
    chomp;
    my ($temp, $name) = split(/ -> /, $_);
    $lowest = $temp if $temp < $lowest;
    $highest = $temp if $temp > $highest;
    $temps{$name} = $temp;

}

foreach my $k (sort keys %temps)
{
    push (@lowest, "$k:$temps{$k}") if $temps{$k} == $lowest;
    unshift (@highest, "$k:$temps{$k}") if $temps{$k} == $highest;

}

#print "lowest: $lowest [@lowest], highest: $highest [@highest]\n";

foreach my $high (@highest)
{
    my ($name1, $temp1) = split(/:/, $high);
    foreach my $low (@lowest)
    {
        my ($name2, $temp2) = split(/:/, $low);
        printf("%s minus %s is %f\n", $name1, $name2, $temp1 -
$temp2);
    }
    print "\n";

}

exit(0);

__DATA__
1.18 -> temp1
1.18 -> temp2
1.26 -> temp3
1.34 -> temp4
1.40 -> temp5
1.50 -> temp6
1.60 -> temp7
1.60 -> temp8
1.60 -> temp9

OUTPUT:
perl perlQ.plx

temp9 minus temp1 is 0.420000
temp9 minus temp2 is 0.420000

temp8 minus temp1 is 0.420000
temp8 minus temp2 is 0.420000

temp7 minus temp1 is 0.420000
temp7 minus temp2 is 0.420000


Thanks a lot your response..
 
J

Jürgen Exner

Deepu said:
Sorry for my incomplete explanation..basically the file is an output
of some calculation and then i need to get the difference from each of
the largest value with each of the smallest value in the file.

That difference would always be the same value because even if there are
multiple instances of largest and smallest, their values and thus their
differences will always be the same.

So, what you are really looking for is
a) the smallest value and the largest value(!) of your list
b) the difference between them
c) a list of all identifiers which have the largest value
d) a list of all identifiers which have the smallest value

Each of these is easy enough to compute.
For example:
* temp7, temp8 & temp9 are largest value in the file & temp1 & temp2
are smallest values in the file.
* Next i need to get the difference & print out as below.
* File has multiple largest & smallest numbers.

temp7 - temp1 -> 0.42
temp7 - temp2 -> 0.42

And then simply create an output of all combinations from list of
largest and list of smallest identifiers and print the pre-computed
difference at the end of each line.

jue
 

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,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top