thanos1983 has asked for the wisdom of the Perl Monks concerning the following question:
Hello again Monks,
I am trying to split a string into an array based on 3 digits (0-9) that can occur infinite times.
Sample of code:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $str = "902 M 903 Textmessage 904 PO 905 S 906 VAS 907 10 908 3629 +909 85290200429/TYPE=thanos\@test.com 910 NA 911 NA 912 NA 913 NA 914 + NA 917 0 918 NA 919 Wed,_01_Feb_2017_19:56:23_GMT 922 NA 923 PO 924 +NA 925 NA 926 07594d85 927 100 928 20170202035623000+08 929 201702020 +35623000+08 930 NA 931 85260531042/TYPE=thanos2\@test.2.com 932 1 934 + 258;3259 920 NA 921 NA 935 NA 936 NA 938 NA 939 NA 940 thanos-local +942 NA 944 NA 945 4880 946 NA 948 NA 950 454000000927816 953 NA 954 1 +3 955 5.3.0 956 NA 957 07594d85 958 NA 961 13 981 NA 982 0 983 852902 +00429/TYPE=thanos3\@test.3.com 984 Wed,_01_Feb_2017_19:56:23_GMT 985 +RegularThanos 986 TEST 987 NA 988 NA 991 NA 992 NA 993 NA 994 1234567 +89 995 NA 996 NA 997 NA 998 NA 603 0E552E92 602 0 617 NA 618 NA 621 N +A 635 NA 636 NA 637 NA 638 NA"; # Create pairs every three digits my @pairs = split(/\s+(\d{3})\s+/, $str); shift @pairs; print Dumper \@pairs; my %hash = @pairs; print Dumper \%hash;
Sample of output:
$VAR1 = { '939' => 'NA', '934' => '258;3259', '913' => 'NA', '931' => '85260531042/TYPE=thanos2@test.2.com', '929' => '20170202035623000+08', '621' => 'NA', '992' => 'NA', '920' => 'NA', '955' => '5.3.0', '946' => 'NA', '903' => 'Textmessage', '950' => '454000000927816', '986' => 'TEST', '985' => 'RegularThanos', '921' => 'NA', '924' => 'NA', '987' => 'NA', '927' => '100', '905' => 'S', '925' => 'NA', '602' => '0', '995' => 'NA', '914' => 'NA', '922' => 'NA', '932' => '1', '603' => '0E552E92', '636' => 'NA', '958' => 'NA', '954' => '13', '983' => '85290200429/TYPE=thanos3@test.3.com', '638' => 'NA', '930' => 'NA', '908' => '3629', '907' => '10', '618' => 'NA', '617' => 'NA', '940' => 'thanos-local', '906' => 'VAS', '944' => 'NA', '919' => 'Wed,_01_Feb_2017_19:56:23_GMT', '998' => 'NA', '637' => 'NA', '981' => 'NA', '918' => 'NA', '910' => 'NA', '991' => 'NA', '993' => 'NA', '936' => 'NA', '904' => 'PO', '926' => '07594d85', '996' => 'NA', '942' => 'NA', '917' => '0', '957' => '07594d85', '912' => 'NA', '909' => '85290200429/TYPE=thanos@test.com', '945' => '4880', '928' => '20170202035623000+08', '953' => 'NA', '994' => '123456789', '956' => 'NA', '938' => 'NA', '635' => 'NA', '911' => 'NA', '948' => 'NA', '961' => '13', '997' => 'NA', '982' => '0', '984' => 'Wed,_01_Feb_2017_19:56:23_GMT', '935' => 'NA', '988' => 'NA', '923' => 'PO' };
It seems to be working, but this is only because I shift the first element of the array. If I parse the array into the hash without shifting the first element of the array then the hash is incomplete and results to the following error:
Odd number of elements in hash assignment at test_2.pl line 20. $VAR1 = { '454000000927816' => '953', '258;3259' => '920', '3629' => '909', '1' => '934', 'NA' => undef, 'PO' => '924', '85260531042/TYPE=thanos2@test.2.com' => '932', '13' => '981', '10' => '908', 'VAS' => '907', 'RegularThanos' => '986', 'TEST' => '987', 'Wed,_01_Feb_2017_19:56:23_GMT' => '985', 'S' => '906', '4880' => '946', '85290200429/TYPE=thanos3@test.3.com' => '984', '902 M' => '903', '100' => '928', '123456789' => '995', '20170202035623000+08' => '930', '85290200429/TYPE=thanos@test.com' => '910', '0' => '617', '07594d85' => '958', '0E552E92' => '602', 'thanos-local' => '942', '5.3.0' => '956', 'Textmessage' => '904' };
Sample of not correctly splited string into array:
$VAR1 = [ '902 M', '903', 'Textmessage', '904', 'PO', '905', 'S', '906', 'VAS', '907', '10', '908', '3629', '909', '85290200429/TYPE=thanos@test.com', '910', 'NA', '911', 'NA', '912', 'NA', '913', 'NA', '914', 'NA', '917', '0', '918', 'NA', '919', 'Wed,_01_Feb_2017_19:56:23_GMT', '922', 'NA', '923', 'PO', '924', 'NA', '925', 'NA', '926', '07594d85', '927', '100', '928', '20170202035623000+08', '929', '20170202035623000+08', '930', 'NA', '931', '85260531042/TYPE=thanos2@test.2.com', '932', '1', '934', '258;3259', '920', 'NA', '921', 'NA', '935', 'NA', '936', 'NA', '938', 'NA', '939', 'NA', '940', 'thanos-local', '942', 'NA', '944', 'NA', '945', '4880', '946', 'NA', '948', 'NA', '950', '454000000927816', '953', 'NA', '954', '13', '955', '5.3.0', '956', 'NA', '957', '07594d85', '958', 'NA', '961', '13', '981', 'NA', '982', '0', '983', '85290200429/TYPE=thanos3@test.3.com', '984', 'Wed,_01_Feb_2017_19:56:23_GMT', '985', 'RegularThanos', '986', 'TEST', '987', 'NA', '988', 'NA', '991', 'NA', '992', 'NA', '993', 'NA', '994', '123456789', '995', 'NA', '996', 'NA', '997', 'NA', '998', 'NA', '603', '0E552E92', '602', '0', '617', 'NA', '618', 'NA', '621', 'NA', '635', 'NA', '636', 'NA', '637', 'NA', '638', 'NA' ];
With the a closer look the first element on the array is wrong, I can not figure it out why this is happening since all the other elements are correctly splited. Can someone who understands perlre help to understand why this is happening?
Thank you in advance for your time and effort.
Update: added the "and" on title.
Update2: adding "that can occur infinite times".
Update3: changing on title from "much" to "match".
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to much and extract string after exact 3 digits
by Corion (Patriarch) on Feb 09, 2017 at 20:26 UTC | |
by thanos1983 (Parson) on Feb 09, 2017 at 20:34 UTC | |
|
Re: How to match and extract string after exact 3 digits [RESOLVED]
by Marshall (Canon) on Feb 09, 2017 at 20:45 UTC | |
by thanos1983 (Parson) on Feb 10, 2017 at 11:50 UTC | |
by Marshall (Canon) on Feb 10, 2017 at 19:15 UTC |