in reply to Re^2: Warning is right or not ?
in thread Warning is right or not ?
... I need the previous value of @data ...
If you mean you need to return the element in the @data array immediately prior to the first element in the array that matches the string passed into the function, I think I would use something like this (Update: an empty string is returned if no match):
>perl -wMstrict -le "print get_IATA('bar'); ;; sub get_IATA { my ($airline) = @_; ;; my @data = qw(fee fie foe foo barbell boff); ;; for my $i (1 .. $#data) { return $data[$i - 1] if $data[$i] =~ m{ \A \Q$airline\E }xms; } return ''; } " foo
Update: Here's a variation that returns every element in the @data array immediately prior to an element in the array that matches the string passed into the function. An empty list is returned if no match.
>perl -wMstrict -le "printf qq{'$_' } for get_IATA('bar'); ;; sub get_IATA { my ($airline) = @_; ;; my @data = qw(fee fie yyy barfly foe fum zzz barbell foo); ;; return map { $data[$_] =~ m{ \A \Q$airline\E }xms ? $data[$_ - 1] : () } 1 .. $#data ; } " 'yyy' 'zzz'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Warning is right or not ?
by Anonymous Monk on Dec 23, 2012 at 19:13 UTC |