Solved

Format problem with code

Posted on 2009-07-11
27
192 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
Comment Utility
Looks like the whiskey is winning, you did not attach the code. :)
0
 

Author Comment

by:MichaelGlancy
Comment Utility
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
Comment Utility
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
Comment Utility
:)
##### 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
Comment Utility
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
Comment Utility
we  dont want to skip a line, we have thousands of lines to sort through :-)
0
 

Author Comment

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

Expert Comment

by:mrjoltcola
Comment Utility
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
Comment Utility
keep them while sorting the array but dont use them at all :_)
0
 
LVL 40

Expert Comment

by:mrjoltcola
Comment Utility
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
Comment Utility
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
Comment Utility
items 2 - 6 is 5 items  :P
0
 

Author Comment

by:MichaelGlancy
Comment Utility
ha ha ha

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

ha ha your right. you need a whisey
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 40

Expert Comment

by:mrjoltcola
Comment Utility
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
Comment Utility
what does
next unless @arr;
mean ?
0
 
LVL 40

Expert Comment

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

Expert Comment

by:mrjoltcola
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
YOU ARE A GOD ! ! ! !   :-)
0
 
LVL 40

Expert Comment

by:mrjoltcola
Comment Utility
>>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
Comment Utility
dave and I have voted. we love you.
0
 

Author Closing Comment

by:MichaelGlancy
Comment Utility
thankyou so much. we love you.
0
 

Author Comment

by:MichaelGlancy
Comment Utility
ok Ive got another question

Im posting it now
0
 
LVL 40

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 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

12 Experts available now in Live!

Get 1:1 Help Now