Help with Perl Q

Discussion in 'Perl Misc' started by Deepu, Jan 25, 2011.

  1. Deepu

    Deepu Guest

    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...
    Deepu, Jan 25, 2011
    #1
    1. Advertising

  2. Deepu <> wrote:
    >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
    Jürgen Exner, Jan 25, 2011
    #2
    1. Advertising

  3. Deepu

    Deepu Guest

    Re: Help with Perl Q (Finding Largest & Smallest numbers & getting difference)

    > 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..
    Deepu, Jan 25, 2011
    #3
  4. Deepu

    Ralph Malph Guest

    On 1/25/2011 9:40 AM, Tad McClellan wrote:
    > Deepu<> wrote:
    >
    >> Subject: Help with Perl Q

    >
    >
    > 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!
    Ralph Malph, Jan 25, 2011
    #4
  5. Deepu

    ccc31807 Guest

    On Jan 25, 2:33 am, Deepu <> wrote:
    > 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
    ccc31807, Jan 25, 2011
    #5
  6. Deepu

    Deepu Guest

    On Jan 25, 8:49 am, ccc31807 <> wrote:
    > On Jan 25, 2:33 am, Deepu <> wrote:
    >
    > > 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



    Thanks a lot your response..
    Deepu, Jan 25, 2011
    #6
  7. Re: Help with Perl Q (Finding Largest & Smallest numbers & getting difference)

    Deepu <> wrote:
    >> 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.


    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
    Jürgen Exner, Jan 26, 2011
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,756
  2. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Jan 23, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    278
    PerlFAQ Server
    Jan 23, 2011
  3. PerlFAQ Server
    Replies:
    0
    Views:
    642
    PerlFAQ Server
    Feb 3, 2011
  4. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Feb 27, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    271
    PerlFAQ Server
    Feb 27, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    654
    PerlFAQ Server
    Apr 4, 2011
Loading...

Share This Page