Brijesh
Brijesh

Reputation: 103

how to subtract 1 from all array elements in perl

I have an array which contains values. I need to subtract 1 from each array elements & save there itself.

For Example:

chop $tve_005;
@words = split (/,/, $tve_005);

and now @words contains:

524210
1713409
311919
1422134
16658312

But infact values need to be used in the rest of the codes are: (subtract by 1 always)

524209
1713408
311918
1422133
16658311

How can I subtract and save it the same array.

Upvotes: 4

Views: 1403

Answers (2)

zb226
zb226

Reputation: 10509

Alternatively to Pradeep's solution, some characters shorter:

#!/usr/bin/perl

my @words = (524210,1713409,311919,1422134,16658312);

$_-- for @words;

Upvotes: 10

Pradeep
Pradeep

Reputation: 3153

Try this

#!/usr/bin/perl

my @words = (524210,1713409,311919,1422134,16658312);

@words = map { $_ - 1 } @words;

Upvotes: 5

Related Questions