Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert single element array to string with dot added before the string in Perl

I have an array which has one element

I am trying to convert that array element to a string with a dot added before the string

my $string = [ 'text' ];

my $convert_str = join(".", @$string);

print $convert_str;

The expected output should be

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

.text

I am getting the output without dot as just:

text

Please help.

Thanks in advance

>Solution :

I don’t know Perl myself so sorry in advance if I got something wrong.

Using join would only work with arrays of 2 or more elements and the separator would be inserted in between the elements. For example

my $string = [ 'text', 'another' ];
my $convert_str = join(".", @$string);
print $convert_str;

would give the result test.another

If you only have 1 elements in the array and you want to prepend a dot before it then this could be done as follows

my $string = [ 'text' ];
my $convert_str = "." . $string->[0];
print $convert_str;

Here, I’m using $string->[0] to access the first element of the array and . to concatenate the dot and place it in front of the string.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading