Question: I am completely new to the Perl programming.
I am trying the split function.
Here is my code: 

```
#!perl;
use strict;
use warnings;
use Data::Dumper;

my @sections = ("Hard Drive", "Multi Drive");
foreach my $secname (sort @sections) 
{
    my $file_name=split / /, $secname;
    print $file_name;
}

```

so I am trying to get the output like only second word only.

output is 
`22`

The expected output is:

```
drive
drive

```

Here I am trying to get second word but it is not giving the expected output.

Please anyone suggest and help me.

Answer: 
When you get unexpected behaviour from Perl function, your first step should be to read the documentation for that function. The [documentation for `split`](http://perldoc.perl.org/functions/split.html) says this:

> 
> Splits the string EXPR into a list of strings and returns the list in
>  list context, or the size of the list in scalar context.
> 
> 
> 

So, in scalar context, `split` returns the number of element in the list, not the actual list. Your code:

```
my $file_name = split / /, $secname;

```

is calling `split` in scalar context (as you are assigning the result to a scalar variable). In order to get the result that you want, yo need to call `split` in list context. There are a number of ways to achieve this.

```
# Assign to an array, take the second element
my @bits = split / /, $secname;
my $file_name = $bits[1];

# Use a list slice to only get the value that you want
my $file_name = (split / /, $secname)[1];

# Use a list assignment
my (undef, $file_name) = split / /, $secname);

```
split converts scalar to list. You tried to split the each values of an list. 

The output `22` results because, it displays the total number of scalar in an list.

For example 

```
@ar = qw(1 4);
$aa = @ar;
print $aa;

```

It results `2`. The same think was rises in your problem.

So you store the splitting value into an array. And the print the expected output by the index key of array.

Try this

```
use strict;
use warnings;
use Data::Dumper;

my @sections = ("Hard Drive", "Multi Drive");
foreach my $secname (sort @sections) 
{
    my @file_name=split / /, $secname;
    print $file_name[1],"\n";
}

```