regex - Array manipulation in Perl -
the scenario follows:
i have dynamically changing text file i'm passing variable capture pattern occurs throughout file. looks this:
my @array1; $file = `cat <file_name>.txt`; if (@array1 = ( $file =~ m/<pattern_match>/g) ) { print "@array1\n"; } the array looks this:
10:38:49 788 56 51 56 61 56 59 56 51 56 80 56 83 56 50 45 42 45 50 45 50 45 43 45 54 10:38:51 788 56 51 56 61 56 59 56 51 56 80 56 83 56 50 45 42 45 50 45 50 45 43 45 54 from above array1 output, pattern of array this:
t1 p1 t1(1) t1(2)...t1(25) t2 p2 t2(1) t2(2)...t2(25) on , forth currently, /g in regex returns set of values occur twice (only because txt file contains pattern number of times). particular pattern occurrence change depending on file name plan pass dynamically.
what intend acheive:
the final result should csv file contains these values in following format:
t1,p1,t1(1),t1(2),...,t1(25) t2,p2,t2(1),t2(2),...,t2(25) on , forth for instance: final csv file should this:
10:38:49,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54 10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54 the delimiter pattern t1 time in format \d\d:\d\d:\d\d
example: 10:38:49, 10:38:51 etc
what have tried far:
use data::dumper; use list::moreutils qw(part); $partitions = 2; $i = 0; print dumper part {$partitions * $i++ / @array1} @array1; in particular case, my $partitions = 2; holds since pattern occurrence in txt file twice, , hence, i'm splitting array two. however, mentioned earlier, pattern occurrence number keeps changing according txt file use.
the question:
how can make code more generic achieve final goal of splitting array multiple equal sized arrays without losing contents of original array, , converting these mini-arrays 1 single csv file?
if there other workaround other array manipulation, please let me know.
thanks in advance.
ps: considered hash of hashes , array of hashes, kind of data structure did not seem healthy solution problem i'm facing right now.
as far can tell, need splice, work fine long know record size , it's constant
the data showed has 52 fields, description of requires 27 fields per record. looks each line has t, p, , t1 .. t24, rather ending @ t25
here's how looks if split data 26-element chunks
use strict; use warnings 'all'; @data = qw/ 10:38:49 788 56 51 56 61 56 59 56 51 56 80 56 83 56 50 45 42 45 50 45 50 45 43 45 54 10:38:51 788 56 51 56 61 56 59 56 51 56 80 56 83 56 50 45 42 45 50 45 50 45 43 45 54 /; while ( @data ) { @set = splice @data, 0, 26; print join(',', @set), "\n"; } output
10:38:49,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54 10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54 if wanted use list::moreutils instead of splice, the natatime function returns iterator same thing splice above
like this
use list::moreutils qw/ natatime /; $iter = natatime 26, @data; while ( @set = $iter->() ) { print join(',', @set), "\n"; } the output identical of program above
note
it wrong start new shell process use cat read file. standard method undefine input record separator $/ this
my $file = { open $fh, '<', '<file_name>.txt' or die "unable open file input: $!"; local $/; <$fh>; }; or if prefer use file::slurper this
use file::slurper qw/ read_binary /; $file = read_binary '<file_name>.txt'; although have install not core module
Comments
Post a Comment