Help with delete an element in array

D

Deepu

Hi All,

I have $key = "SA-SB-SC-SD" separated by '-'
I am trying to delete SC and the output should be
$test="SA-SB-SD".

But when I do as shown below, the output generated is
$test="SA-SB--SD". I am not getting the reason for getting extra
'-'.


$key="SA-SB-SC-SD";

@test=split(/-/,$key);

Delete $test[1];

$test=join '-',@test;

Print $test;


Thanks for the time.
 
D

David Squire

Deepu said:
Hi All,

I have $key = "SA-SB-SC-SD" separated by '-'
I am trying to delete SC and the output should be
$test="SA-SB-SD".

But when I do as shown below, the output generated is
$test="SA-SB--SD". I am not getting the reason for getting extra
'-'.


$key="SA-SB-SC-SD";

@test=split(/-/,$key);

Delete $test[1];

$test=join '-',@test;

Print $test;

This won't compile, let alone being safe under "use strict;" and "use
warnings;". Please post your real code. Don't just type in nonsense.


DS
 
D

David Squire

David said:
Deepu said:
Hi All,

I have $key = "SA-SB-SC-SD" separated by '-'
I am trying to delete SC and the output should be
$test="SA-SB-SD".

But when I do as shown below, the output generated is
$test="SA-SB--SD". I am not getting the reason for getting extra
'-'.


$key="SA-SB-SC-SD";

@test=split(/-/,$key);

Delete $test[1];

$test=join '-',@test;

Print $test;

This won't compile, let alone being safe under "use strict;" and "use
warnings;". Please post your real code. Don't just type in nonsense.

I'm feeling (a bit) generous. Reading the documentation for the Perl
function 'delete' will tell you what is going on - and point you in the
direction of the function you seem to want: splice.

perldoc -f delete
perldoc -f splice


DS
 
D

Deepu

Thanks for the info. I used Splice now and it works.

I used:

#!/usr/bin/perl

my $key = "SA-SB-SC-SD";
my @test = split (/-/,$key);
splice(@test,1,1);
my $test = join '-',@test;
print $test;


David said:
David said:
Deepu said:
Hi All,

I have $key = "SA-SB-SC-SD" separated by '-'
I am trying to delete SC and the output should be
$test="SA-SB-SD".

But when I do as shown below, the output generated is
$test="SA-SB--SD". I am not getting the reason for getting extra
'-'.


$key="SA-SB-SC-SD";

@test=split(/-/,$key);

Delete $test[1];

$test=join '-',@test;

Print $test;

This won't compile, let alone being safe under "use strict;" and "use
warnings;". Please post your real code. Don't just type in nonsense.

I'm feeling (a bit) generous. Reading the documentation for the Perl
function 'delete' will tell you what is going on - and point you in the
direction of the function you seem to want: splice.

perldoc -f delete
perldoc -f splice


DS
 
D

David Squire

Deepu wrote:

[top--posting corrected. Please don't do that.]
David said:
David said:
Deepu wrote:
Hi All,

I have $key = "SA-SB-SC-SD" separated by '-'
I am trying to delete SC and the output should be
$test="SA-SB-SD".

But when I do as shown below, the output generated is
$test="SA-SB--SD". I am not getting the reason for getting extra
'-'.


$key="SA-SB-SC-SD";

@test=split(/-/,$key);

Delete $test[1];

$test=join '-',@test;

Print $test;
This won't compile, let alone being safe under "use strict;" and "use
warnings;". Please post your real code.
[snip]
perldoc -f delete
perldoc -f splice


DS

Thanks for the info. I used Splice now and it works.

Good. Please get into the habit of reading the documentation *before*
posting here.
I used:

#!/usr/bin/perl

missing:

use strict; # though your code is indeed now strict-safe. Thanks for that.
use warnings;
my $key = "SA-SB-SC-SD";

no need for double-quotes here.
my @test = split (/-/,$key);
splice(@test,1,1);
my $test = join '-',@test;
print $test;

Just FYI, this will also work in your case:

my $pos_to_replace = 1;
$key =~ s/(([A-Z]{2}-){$pos_to_replace})[A-Z]{2}-/$1/;


DS
 
D

David Squire

David said:
Deepu said:
my $key = "SA-SB-SC-SD";
[snip]

Just FYI, this will also work in your case:

my $pos_to_replace = 1;
$key =~ s/(([A-Z]{2}-){$pos_to_replace})[A-Z]{2}-/$1/;

Actually, no :( This will fail if you try to replace this last element
of $key.


DS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top