Add year to array in perl

Need assistance in perl scripting .
Is there a logic that i can implement to check the current year and add to an array . ex Current year is 2014 . If 2014 is not present add this year to array .

@yar = qw(2013 2012 2011);
foreach (@yar){
print "Year: $_ ";
}
@yar = qw(2014 2013 2012 2011);
foreach (@yar){
print "Year: $_ ";
}

It is always a good idea to use "use strict" and "my $variable" declarations. Indentation helps too. As for your question, try this:

my @yar = qw(2013 2012 2011);
my $curr_year = (localtime)[5]+1900;
unshift @yar, $curr_year unless grep {$_ == $curr_year} @yar;
foreach (@yar){
  print "Year: $_ ";
}

This works great . But the only concern was after 2014 if we want to add for next year 2015 which has to give me 2011,2012,2013,2014,2015. Is there a way to get it.

Try something like this:

my @yar = qw(2014 2013 2012 2011);
my $next_year = (localtime)[5]+1901;
unshift @yar, $next_year unless grep {$_ == $next_year} @yar;
foreach (@yar){
  print "Year: $_ ";
}

Thank you bartus11

I wanted a logic where every year it should automatically take the year and place it into the array

Not by adding the 2014 into the array .

Currently 2013 2012 2012
After logic 2014 2013 2012 2011
Next year 2015 2014 2013 2012 2011
Following year 2016 2014 2013 2012 2011

This is exactly what my $curr_year = (localtime)[5]+1900; is doing. It extracts current year from the "localtime" function, so the first code will add current year to the array every time year's number increases.

Nice . In 2015 It will give 2015 2013 2012 2011 . Correct me if I am wrong.

---------- Post updated at 04:58 PM ---------- Previous update was at 10:47 AM ----------

Thank again bartus11
Finally found a way to get what i wanted with your help . Here is the code

my $yar = 2011;
my $curr_year = (localtime)[5]+1901;
until ($yar >= $curr_year) {
    print " $yar","\n";
    $yar++;
}