Hi All,
I have a problem in getting a logic for a code I am working on, All I have to do is insert a line for each missing sequential numbers.
I have a 8 columns in a file of which the last 5 is of my interest. I have spliced the 5 columns and made it to a hash.
Now the spliced array is like this :
1234522
1567811
18810122
2133443
2455613
this would continue further toaround1000's of lines
Which actually looks like this if comma separated
,
1,23,45,2,2
1,56,78,1,1
1,88,101,2,2
2,13,34,4,3
2,45,56,1,3
I have the first column as a key and rest in a array as values.
What I would need is something like this
1,1,22,2,2
1,23,45,2,2
1,46,55,2,2
1,56,78,1,1
1,79,87,2,2
1,88,101,2,2
2,1,12,2,2
2,13,34,4,3
2,35,44,2,2
2,45,56,1,3
I would like to insert rows, so that there is no gaps in the sequence, and is continuous until it finishes the particular key>say 1/2.
Any suggestions or ideas?
Though it doesn't do much; my little code is here
!/software/bin/perl
use strict;
use warnings ;
while(<>){
my @line = split(/,/,$_);
my @splice = splice(@line, 3,8);
my $chr = shift @splice;#shifts the first element
my %hash;
$hash{$chr} = \@splice;
foreach my $key (sort keys %hash){
print $key, "," , $hash{$key}[0] ,",", $hash{$key}[1], "\n";
}
}
The input would be:
1,34,1,23,45,2,2
35,45,1,56,78,1,1
46,56,1,88,101,2,2
57,68,2,13,34,4,3
69,78,2,45,56,1,3
Thanks a lot in advance,
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.