Solved

Format problem with code

Posted on 2009-07-11
27
202 Views
Last Modified: 2012-05-07
Ok, you beautifull Perl wizards. Its Saturday night and my collegue and I, while still at work, are drinking whiskey and trying to "hack" code. What a laugh.

I / we have a problem. First, this code operates on all data read from the text file. Please could you help me / us by   stopping this data from using the first data entry it takes in, especially at Step 2, it should only sort items 2 - 6 into ascending order.
At step three, the first item from step 2 should subtract 0, in order to give the value of step 2. their should be seven differences.
the average should be calculated to only 2 decimal places.

We have tried so hard tonight to mess with this code and we cant make it work. Please could you help ?

:-) Michael
0
Comment
Question by:MichaelGlancy
  • 15
  • 12
27 Comments
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24832347
Looks like the whiskey is winning, you did not attach the code. :)
0
 

Author Comment

by:MichaelGlancy
ID: 24832349
whiskey g=h has won :-)
#!/usr/bin/perl

use strict;

use warnings;

use Data::Dumper;

use List::Util qw(sum);

 

##### Open files

open(my $IN,"invim2.vim") or die "Could not open input: $!\n";

open(my $OUT,">outvim2.vim") or die "Could not open output: $!\n";

 

##### Step 1: read a line, and split it on comma

while(<$IN>){

        chomp;

        my @arr = split /,/;

        next unless @arr;

        

        ##### Step 2: Sort data into ascending order

        @arr = sort {$a <=> $b} @arr;

        

        ##### Step 3: Calculate difference between neighboring pairs

        my @diff = map {$arr[$_] - $arr[$_-1]} (1..$#arr);

        

        ##### Step 4: Calucate amount in each group

        my @groups = (0)x10;

        $groups[$_/100]++ foreach (@arr);

        

        ##### Step 6: Calculate average

        my $average = sum(@arr)/@arr;

        

        ##### Step 5: Print results

        print $OUT join("    ", @arr) . "    avg $average     group @groups\n";

        print $OUT join("    ", @diff) . "\n";

        print $OUT "\n";

}

 

 

close($IN);

close($OUT);

Open in new window

0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24832350
One way to skip a line in a file, for example:

open(FILE, "<input.txt");
<FILE>;   # discard first line
my @lines = <FILE>;    # read the rest
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24832356
:)
##### Step 1: read a line, and split it on comma

<$IN>;   # <-- ADD THIS TO SKIP THE 1st LINE

while(<$IN>){

Open in new window

0
 

Author Comment

by:MichaelGlancy
ID: 24832357
no not to skip a line, just the first data on the line

like

xxx,455,667,322,1223,445,667,333,xxx

we want not to include the xxx's
0
 

Author Comment

by:MichaelGlancy
ID: 24832359
we  dont want to skip a line, we have thousands of lines to sort through :-)
0
 

Author Comment

by:MichaelGlancy
ID: 24832365
you do know you guys are f######g excellent ?
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24832367
You want to discard the 1st and last element completely, or keep them while sorting the rest of the array?
0
 

Author Comment

by:MichaelGlancy
ID: 24832369
keep them while sorting the array but dont use them at all :_)
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24832399
Ok maybe its because I 'ave no whiskey here but lets clarify..

>>but dont use them at all

Don't use them in any step at all, but just print them at the last?
0
 

Author Comment

by:MichaelGlancy
ID: 24832403
no dont print them off. Would you like a whiskey ?

the array can them in, but just dont do anything with them just now.

Just process items 2 - 6 (seven items)

0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24832411
items 2 - 6 is 5 items  :P
0
 

Author Comment

by:MichaelGlancy
ID: 24832416
ha ha ha

item 2, 3, 4, 5, 6, 7 and 8

ha ha your right. you need a whisey
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24832430
Ok, for starters, this is one way to discard 1st and last items.

Add a shift and pop to drop the items prior to processing.
my @arr = split /,/;

shift @arr; pop @arr;

next unless @arr;

Open in new window

0
 

Author Comment

by:MichaelGlancy
ID: 24832435
what does
next unless @arr;
mean ?
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24832443
Its line #15 from your original code. It means skip any empty arrays.
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24832444
Now regarding the differences. If you have 7 numbers, and you want 7 differences, then I assume you treat the 1st element as a neighbor of the last (wrapping at the end?)

Your current code produces 6 differences because it starts at 1 (not 0)
0
 

Author Comment

by:MichaelGlancy
ID: 24832452
ok Im not sure what you meant about nieghbours and niether is dave too sure.

we are dealing with items 2,3,4,5,6,7 and 8 (as you pointed out :-)

so, the first difference is item 2 - zero. then item 3 - item 2 and so and so forth until item 8 - item 7.

0
 

Author Comment

by:MichaelGlancy
ID: 24832454
we need the value difference between the elements

it represents the actual difference between the globular protein molecules. the first item is irrelevent but the second item , we need the distance of that from the start ( 0 ), you see ?
0
 

Author Comment

by:MichaelGlancy
ID: 24832457
you perl guys are soooooo cool. honestly, you are helping us progress soooo much
0
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 500 total points
ID: 24832459
Ok, this has steps 2 and 3 working as requested. Not sure about the requirements on the rest, you need to clarify.

#!/usr/bin/perl

use strict;

use warnings;

use Data::Dumper;

use List::Util qw(sum);
 

##### Open files

open(my $IN,"invim2.vim") or die "Could not open input: $!\n";

open(my $OUT,">outvim2.vim") or die "Could not open output: $!\n";
 

##### Step 1: read a line, and split it on comma

while(<$IN>){

        chomp;

        my @arr = split /,/;

        next unless @arr;
 

        # discard 1st and last item

        shift @arr; pop @arr;
 

        ##### Step 2: Sort data into ascending order

        @arr = sort {$a <=> $b} @arr;

        print "arr @arr\n";
 

        ##### Step 3: Calculate difference between neighboring pairs

        my @diff = ($arr[0], map {$arr[$_] - $arr[$_-1]} (1..$#arr));

        print "diff @diff\n";
 

        ##### Step 4: Calucate amount in each group

        my @groups = (0)x10;

        $groups[$_/100]++ foreach (@arr);
 

        ##### Step 6: Calculate average

        my $average = sum(@arr)/@arr;
 

        ##### Step 5: Print results

        print $OUT join("    ", @arr) . "    avg $average     group @groups\n";

        print $OUT join("    ", @diff) . "\n";

        print $OUT "\n";

}
 
 

close($IN);

close($OUT);

Open in new window

0
 

Author Comment

by:MichaelGlancy
ID: 24832464
YOU ARE A GOD ! ! ! !   :-)
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24832466
>>you perl guys are soooooo cool. honestly, you are helping us progress soooo much

We know. Say hi to dave, haven't seen him in ages :)
0
 

Author Comment

by:MichaelGlancy
ID: 24832467
dave and I have voted. we love you.
0
 

Author Closing Comment

by:MichaelGlancy
ID: 31602482
thankyou so much. we love you.
0
 

Author Comment

by:MichaelGlancy
ID: 24832471
ok Ive got another question

Im posting it now
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24832474
Cheers. Don't have too much or you might not make it home. :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
perl to mysql 5 130
Validating the data using Oracle DBD module. 5 65
Perl - Mawk 2 87
How to translate this 2-line while loop into C from Perl? 8 74
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now