ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm trying to understand the below syntax from a perl script that is part of a subroutine checking for free space. I know "$fs" is a variable and have a vague idea that inplace variable is being done here (please correct me if this is wrong) but have no clue what "[3]" means. I request the Gurus to explain with an example. Thanks in advance.
First you need to understand how the split operator works. From man perlrequick:
Code:
The split operator
"split /regex/, string" splits "string" into a list of substrings and returns that list. The regex
determines the character sequence that "string" is split with respect to. For example, to split a
string into words, use
$x = "Calvin and Hobbes";
@word = split /\s+/, $x; # $word[0] = 'Calvin'
# $word[1] = 'and'
# $word[2] = 'Hobbes'
...
Next, think of the structure of that line of code and try to figure out what it is doing. Here is a very simple give-away, give it a try!
Code:
my $fs = 'one two three four five';
$fs = (split /\s+/, $fs)[3];
print $fs;
When in doubt, writing a quick script to demonstrate what it does is often faster than looking for an explanation!
Good luck!
Last edited by astrogeek; 09-19-2018 at 01:20 PM.
Reason: typos
Just to add on to what astrogeek was saying, the purpose of writing split as
Code:
(split /\s+/, $fs)[3]
is to put it into what is known as list context. If you try to write
Code:
split(/\s+/, $fs)[3]
you will get a syntax error. [3] simply means get the value of the list at the third index. Since Perl uses 0-based indices, that will give you the fourth element in your list.
Also $fs is surrounded with ( and ) as split is expected to return an array
If you ommit the parenthesis it will assign the number of elements from array returned by split (eg: 1), it's perl normal behaviour in scalar context.
By using parenthesis, you explicitly set a list context for the assignement
[edit]
Just tested, I was wrong, parenthesis are not needed here as the returned value is already a scalar extracted from the split array by using [n] array index selector
I guess the programmer used parenthesis as an habit of style working with split assignement
Just tested, I was wrong, parenthesis are not needed here as the returned value is already a scalar extracted from the split array by using [n] array index selector
I guess the programmer used parenthesis as an habit of style working with split assignement
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.