in reply to Re^3: Repeating a capture group pattern within a pattern
in thread Repeating a capture group pattern within a pattern

Thanks. Is there a way to use split() on blocks of 21 items at a time? The readings come in blocks of 21 data points with the data points being determined by position in the sequence: Pressure, Geop Height, Temperature, Dew Point, Humidity, Wind Direction, Wind Speed MS, Wind UMS, Wind VMS, Precipitation Amount, Total Cloud Cover, Low Cloud Cover, Medium Cloud Cover, High Cloud Cover, Radiation Global, Radiation Global Accumulation, Radiation Net Surface LW Accumulation, Radiation Net Surface SW Accumulation, Radiation SW Accumulation, Visibility, and Wind Gusts.

Replies are listed 'Best First'.
Re^5: Repeating a capture group pattern within a pattern
by tybalt89 (Monsignor) on Jul 15, 2024 at 18:04 UTC

    I would probably do something like this to make an array of hashrefs.

    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11160609 use warnings; use List::AllUtils qw( pairwise bundle_by ); my @keys = split /, /, 'Pressure, Geop Height, Temperature, Dew Point, + Humidity, Wind Direction, Wind Speed MS, Wind UMS, Wind VMS, Precipi +tation Amount, Total Cloud Cover, Low Cloud Cover, Medium Cloud Cover +, High Cloud Cover, Radiation Global, Radiation Global Accumulation, +Radiation Net Surface LW Accumulation, Radiation Net Surface SW Accum +ulation, Radiation SW Accumulation, Visibility, Wind Gusts'; my @datasets = bundle_by { +{ pairwise { $a, $b } @keys, @_ } } 21, split ' ', getdata(); use Data::Dump 'dd'; dd @datasets[0 .. 2]; sub getdata { return '1016.1 7.7 20.6 13.8 72.9 215.0 6.64 3.94 5.33 0.0 31.3 31.3 0 +.0 0.0 ... 10727690.0 38242.3 10.8'; }

    And here's what the first 3 elements of the answer array are:

    ( { "Dew Point" => 13.8, "Geop Height" => 7.7, "High Cloud Cover" => "0.0", "Humidity" => 72.9, "Low Cloud Cover" => 31.3, "Medium Cloud Cover" => "0.0", "Precipitation Amount" => "0.0", "Pressure" => 1016.1, "Radiation Global" => 716.7, "Radiation Global Accumulation" => "8461717.0", "Radiation Net Surface LW Accumulation" => "NaN", "Radiation Net Surface SW Accumulation" => "7750386.0", "Radiation SW Accumulation" => "2224256.0", "Temperature" => 20.6, "Total Cloud Cover" => 31.3, "Visibility" => 38507.5, "Wind Direction" => "215.0", "Wind Gusts" => 10.9, "Wind Speed MS" => 6.64, "Wind UMS" => 3.94, "Wind VMS" => 5.33, }, { "Dew Point" => 13.7, "Geop Height" => 7.7, "High Cloud Cover" => "0.0", "Humidity" => 71.1, "Low Cloud Cover" => 2.1, "Medium Cloud Cover" => "0.0", "Precipitation Amount" => "0.0", "Pressure" => 1016.4, "Radiation Global" => 727.6, "Radiation Global Accumulation" => "11081057.0", "Radiation Net Surface LW Accumulation" => "NaN", "Radiation Net Surface SW Accumulation" => "10146792.0", "Radiation SW Accumulation" => 2372829.8, "Temperature" => 21.1, "Total Cloud Cover" => 2.1, "Visibility" => 40682.2, "Wind Direction" => "222.0", "Wind Gusts" => 11.2, "Wind Speed MS" => 6.82, "Wind UMS" => 4.67, "Wind VMS" => 4.95, }, { "Dew Point" => 13.2, "Geop Height" => 7.7, "High Cloud Cover" => "0.0", "Humidity" => 67.7, "Low Cloud Cover" => 0.8, "Medium Cloud Cover" => "0.0", "Precipitation Amount" => "0.0", "Pressure" => 1016.4, "Radiation Global" => 749.9, "Radiation Global Accumulation" => "13780637.0", "Radiation Net Surface LW Accumulation" => "NaN", "Radiation Net Surface SW Accumulation" => "12614863.0", "Radiation SW Accumulation" => 4011495.3, "Temperature" => 21.6, "Total Cloud Cover" => 0.8, "Visibility" => 44407.4, "Wind Direction" => "220.0", "Wind Gusts" => 11.3, "Wind Speed MS" => 6.71, "Wind UMS" => 4.37, "Wind VMS" => 5.1, }, )
Re^5: Repeating a capture group pattern within a pattern
by Corion (Patriarch) on Jul 15, 2024 at 11:49 UTC

    I would first split and then process in chunks of 21 items:

    my @values = split /\s+/, $weatherdata; croak "Invalid item count in weather data" unless @values % 21 == 0; while(my @row = splice @values, 0, 21 ) { .... }
Re^5: Repeating a capture group pattern within a pattern
by talexb (Chancellor) on Jul 16, 2024 at 00:29 UTC
      Is there a way to use split on blocks of 21 items at a time?

    Sure -- ask for the first 22 blocks (n+1) using split, then re-do the process with the last block (which will will contain the remaining bits). Repeat until you're out of blocks.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.