Match Array for value in reference array - Perl

I have the following array of array references:

my @holidays = [[2012,'01','02'],[2012,'01','16'],[2012,'02','20'],[2012,'04','16'],[2012,'05','28'],[2012,'07','04'],[2012,'09','03'],[2012,'10','08'],[2012,'11','12'],[2012,'11','22'],[2012,'12','25']]; 

Which IRS are recognized as legal holidays during 2012. I would like to map the @dueDate array to a value in that array and return 1 or true if present.

  while ($holidays[@dueDate]){ print ("Found Holiday \t join('-',@dueDate)"); @dueDate = Add_Delta_Days(@dueDate, 1); if ( Day_of_Week(@dueDate) > 5){ @dueDate = Monday_of_Week((Week_Number(@dueDate)+1), $dueDate[0]); } } 

My current attempt - the condition of the while statement will never be true. I tried several different combinations of summarizing and dereferencing the holidays to no avail.

What would be the best way to manipulate the evaluation inside the while statement so that the block is executed when @dueDate contains the date in my array above.

Note: @dueDate is a standard array of Date :: Calc - (Year, Month, Day)

+4
source share
4 answers

That should put you on the right track. Two problems that I see with your code: an array of arrays should have regular parentheses in the outer part and use the ~~ operator to compare arrays for equality.

 my @holidays = ([2012,'01','02'],[2012,'01','16'],[2012,'02','20'],[2012,'04','16'], [2012,'05','28'],[2012,'07','04'],[2012,'09','03'],[2012,'10','08'],[2012,'11','12'], [2012,'11','22'],[2012,'12','25']); my $i; my @duedate = [2012, '01', '02']; for ($i = 0; $i < @holidays; $i++) { if (@holidays[$i] ~~ @duedate) { print "matched!!"; } } 
+3
source

At first,

 my @holidays = [[2012,'01','02'],...,[2012,'12','25']]; 

it should be

 my @holidays = ([2012,'01','02'],...,[2012,'12','25']); 

You create an array with one element.


Probably the best way to achieve what you want is to use a hash.

 my %holidays = map { join('-', @$_) => 1 } @holidays; 

Then you will need

 while ($holidays{join('-', @dueDate)}) { my $dow = Day_of_Week(@dueDate); @dueDate = Add_Delta_Days(@dueDate, $dow == 5 || $dow == 6 ? 8 - $dow : 1); } 
+2
source

This is my answer when working on Perl 5.14, also I use smartmatching ~~ operator to compare two arrays.

You assign array @holidays = [[2012, '01 ',' 02 '],]; incorrectly, you are assigning an anonymous array [['2012', '01', '02'],] for the first @holidays element.

 use v5.14; my @holidays = ( ['2012', '01', '02'], ['2012', '01', '16'] ); my @due_date = ( '2012', '01', '16' ); for my $holiday (@holidays) { if (@$holiday ~~ @due_date) { say "holiday match"; } } 
+2
source

Ok, a few things:

1: Lists are contained in parentheses, and array literal references are written between the brackets. So you should have:

my @holidays = ([2012,'01','02'],[2012,'01','16'],[2012,'02','20'],[2012,'04','16'],[2012,'05','28'], [2012,'07','04'],[2012,'09','03'],[2012,'10','08'],[2012,'11','12'],[2012,'11','22'],[2012,'12','25']);

2: When you watch $holidays[@dueDate] , you call everything in a scalar context . In particular, since @dueDate has three elements, you only look at $holidays[3] .

3: If you are not writing a piece of code, always use strict; and use warnings; .

So you want something like this:

 use strict; use warnings; my @holidays = ([2012,'01','02'],[2012,'01','16'],[2012,'02','20'],[2012,'04','16'],[2012,'05','28'], [2012,'07','04'],[2012,'09','03'],[2012,'10','08'],[2012,'11','12'],[2012,'11','22'],[2012,'12','25']); my @dueDates=([2012,'01','01'],[2012,'01','02'],[2012,'01','03']); #Or whatever my @due_dates_that_are_holidays=(); foreach my $due_date(@dueDates) { foreach my $holiday(@holidays) { my ($h_y,$h_m,$h_d) =@ $holiday; #Capturing year month and day from the array reference my ($d_y,$d_m,$d_d) =@ $due_date; #Ditto for the due date if($h_y == $d_y and $h_m eq $d_m and $h_d eq $d_d) { push @due_dates_that_are_holidays,$due_date; } } } print join("-",@{$_}) . "\n" foreach(@due_dates_that_are_holidays); 

The above code outputs the following result:

 2012-01-02 
0
source

All Articles