Cody Fendant has asked for the wisdom of the Perl Monks concerning the following question:

I have a situation where I'm reading from an unreliable source of data.

Essentially, if I split a line and there are only 6 elements, I've got a problem, because there should be 7. The extra one should be at index 1.

['foo' , 'bar', 'baz', 'etc', 'etc', 'etc'] ##problem line ['foo' , '', 'bar', baz', 'etc', 'etc', 'etc'] ## problem now fixe +d.

What's the elegant or Perlish way to do this? Doing it this way:

@newline = ($line[0], '',@line[ 1, 2, 3, 4, 5 ]);

Doesn't seem so great.

Replies are listed 'Best First'.
Re: Ways to add an item into the middle of an array?
by choroba (Cardinal) on Nov 20, 2015 at 00:49 UTC
    See splice:
    #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my @line = qw( foo bar baz etc etc etc ); splice @line, 1, 0, ''; # Insert at position 1, replace 0 elements. print Dumper \@line;
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      Thank you! I knew there was a function to do that out there...
Re: Ways to add an item into the middle of an array?
by vinoth.ree (Monsignor) on Nov 20, 2015 at 04:01 UTC