UbuntuThis forum is for the discussion of Ubuntu Linux.
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 use split function without assigning it to any variable. So, it should by default be assigned to @_. Now when I give command to print its elements as shown below, I get null.
Code:
print ($_[1]);
I tried all different ways to see if I get the output but to no avail. i get null only.
Keeping everyithng same and now assigning to a variable like @check, it works.
I use split function without assigning it to any variable. So, it should by default be assigned to @_. Now when I give command to print its elements as shown below, I get null.
Code:
print ($_[1]);
I tried all different ways to see if I get the output but to no avail. i get null only.
Keeping everyithng same and now assigning to a variable like @check, it works.
This would be more interesting if you showed your split() command and the data you supply to it.
Also did you display the result immediately after before @_ could have been changed?
I use split function without assigning it to any variable. So, it should by default be assigned to @_. Now when I give command to print its elements as shown below, I get null.
Code:
print ($_[1]);
I tried all different ways to see if I get the output but to no avail. i get null only.
Keeping everyithng same and now assigning to a variable like @check, it works.
Why do you assume split assigns to @_ ?
Nowhere in the docs does it say that it will do so.
If you do not save the returned value, you will not be able to access it.
Why do you assume split assigns to @_ ?
Nowhere in the docs does it say that it will do so.
If you do not save the returned value, you will not be able to access it.
This I say after reading the book: UNIX - concepts and applications by Sumitabha Das. There a few examples are also mentioned showing this.
As you say that you haven't seen in any docs, let me check internet and find out.
perldoc -f split
split /PATTERN/,EXPR,LIMIT
split /PATTERN/,EXPR
split /PATTERN/
split Splits the string EXPR into a list of strings and
returns that list. By default, empty leading fields
are preserved, and empty trailing ones are deleted.
(If all fields are empty, they are considered to be
trailing.)
In scalar context, returns the number of fields
found and splits into the @_ array. .....
you have different documentation to me. In my documentation there is no mention of setting @_ as a result of calling split in scalar context.
Code:
split /PATTERN/,EXPR,LIMIT
split /PATTERN/,EXPR
split /PATTERN/
split
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.
If only PATTERN is given, EXPR defaults to $_ .
Anything in EXPR that matches PATTERN is taken to be a separator that separates the EXPR into substrings (called "fields") that do not include the separator. Note that a separator may be longer than one character or even have no characters at all (the empty string, which is a zero-width match).
...
in scalar context, returns the number of fields found and splits into the @_ array.
If this is correct for your version of perl, then you need to let the interpreter know you want scalar context by assigning to a scalar variable:
Code:
my $x = split /\s/, $string;
print $_[0]; # @_ set by call to split ... ???
NB. Your documentation is different to that at perldoc.perl.org, so the above may not work. It definitely does NOT work on my system.
Last edited by bonnydeal; 07-19-2013 at 05:01 AM.
Reason: additional info
$ ./split_test.plx
Use of implicit split to @_ is deprecated at ./split_test.plx line 5.
Name "main::n" used only once: possible typo at ./split_test.plx line 8.
1 Fri
2 Jul
3 19
4 11:47:58
5 BST
6 2013
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.