LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-19-2018, 09:58 AM   #1
new_user3085
LQ Newbie
 
Registered: Sep 2016
Posts: 21

Rep: Reputation: Disabled
Need help understanding this Perl syntax


Hi Everyone,

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.

Code:
($fs) = (split /\s+/, $fs)[3];
 
Old 09-19-2018, 01:15 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
No guru needed!

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
 
Old 09-19-2018, 01:52 PM   #3
individual
Member
 
Registered: Jul 2018
Posts: 315
Blog Entries: 1

Rep: Reputation: 233Reputation: 233Reputation: 233
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.
 
Old 09-19-2018, 05:37 PM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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

Last edited by keefaz; 09-19-2018 at 05:46 PM.
 
1 members found this post helpful.
Old 09-19-2018, 09:20 PM   #5
individual
Member
 
Registered: Jul 2018
Posts: 315
Blog Entries: 1

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by keefaz View Post
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
Good job spotting your mistake and correcting.
 
Old 09-26-2018, 11:07 AM   #6
igadoter
Senior Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: many, primary Slackware
Posts: 2,717
Blog Entries: 1

Rep: Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625
Run this code
Code:
#!/usr/bin/perl

@foo = (1, "lama", "camel",4);

($x, $y)= @foo;

print "$x, $y";
it should clarify things for you and you will understand (as me) the '($fs)' on the left-hand side.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Understanding Syntax for 2 Linux commands. swamprat Linux - Newbie 2 07-15-2017 02:03 PM
Need Help Understanding find Syntax linux4evr5581 Linux - Newbie 9 12-28-2016 11:11 PM
perl: Context or Syntax merana Programming 4 12-08-2009 12:17 AM
Please help me in understanding this perl code. Raheel Hassan Programming 6 03-21-2009 06:13 PM
perl syntax jenny_psion Programming 2 08-13-2003 12:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration