Format problem with code

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
Europa MacDonaldChief slayer of dragonsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mrjoltcolaCommented:
Looks like the whiskey is winning, you did not attach the code. :)
0
Europa MacDonaldChief slayer of dragonsAuthor Commented:
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
mrjoltcolaCommented:
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
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

mrjoltcolaCommented:
:)
##### 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
Europa MacDonaldChief slayer of dragonsAuthor Commented:
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
Europa MacDonaldChief slayer of dragonsAuthor Commented:
we  dont want to skip a line, we have thousands of lines to sort through :-)
0
Europa MacDonaldChief slayer of dragonsAuthor Commented:
you do know you guys are f######g excellent ?
0
mrjoltcolaCommented:
You want to discard the 1st and last element completely, or keep them while sorting the rest of the array?
0
Europa MacDonaldChief slayer of dragonsAuthor Commented:
keep them while sorting the array but dont use them at all :_)
0
mrjoltcolaCommented:
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
Europa MacDonaldChief slayer of dragonsAuthor Commented:
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
mrjoltcolaCommented:
items 2 - 6 is 5 items  :P
0
Europa MacDonaldChief slayer of dragonsAuthor Commented:
ha ha ha

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

ha ha your right. you need a whisey
0
mrjoltcolaCommented:
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
Europa MacDonaldChief slayer of dragonsAuthor Commented:
what does
next unless @arr;
mean ?
0
mrjoltcolaCommented:
Its line #15 from your original code. It means skip any empty arrays.
0
mrjoltcolaCommented:
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
Europa MacDonaldChief slayer of dragonsAuthor Commented:
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
Europa MacDonaldChief slayer of dragonsAuthor Commented:
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
Europa MacDonaldChief slayer of dragonsAuthor Commented:
you perl guys are soooooo cool. honestly, you are helping us progress soooo much
0
mrjoltcolaCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Europa MacDonaldChief slayer of dragonsAuthor Commented:
YOU ARE A GOD ! ! ! !   :-)
0
mrjoltcolaCommented:
>>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
Europa MacDonaldChief slayer of dragonsAuthor Commented:
dave and I have voted. we love you.
0
Europa MacDonaldChief slayer of dragonsAuthor Commented:
thankyou so much. we love you.
0
Europa MacDonaldChief slayer of dragonsAuthor Commented:
ok Ive got another question

Im posting it now
0
mrjoltcolaCommented:
Cheers. Don't have too much or you might not make it home. :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.