LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu
User Name
Password
Ubuntu This forum is for the discussion of Ubuntu Linux.

Notices


Reply
  Search this Thread
Old 07-06-2013, 10:01 PM   #1
ravisingh1
Member
 
Registered: Apr 2013
Location: Mumbai
Distribution: Ubuntu13.10
Posts: 291

Rep: Reputation: Disabled
@_ variable of perl not working in ubuntu12.10


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.
 
Old 07-08-2013, 05:24 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by ravisingh1 View Post
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?
 
Old 07-18-2013, 08:53 AM   #3
bonnydeal
Member
 
Registered: Feb 2006
Posts: 47

Rep: Reputation: 29
Quote:
Originally Posted by ravisingh1 View Post
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.
 
Old 07-18-2013, 12:22 PM   #4
ravisingh1
Member
 
Registered: Apr 2013
Location: Mumbai
Distribution: Ubuntu13.10
Posts: 291

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by bonnydeal View Post
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.
 
Old 07-18-2013, 01:30 PM   #5
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
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. .....
And there it is.
 
Old 07-19-2013, 04:41 AM   #6
bonnydeal
Member
 
Registered: Feb 2006
Posts: 47

Rep: Reputation: 29
Quote:
Originally Posted by linosaurusroot View Post
And there it is.
Sorry,

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).
...
This is also the same in the documentation at
perldoc.perl.org.

However in your documentation it says:

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
 
Old 07-19-2013, 05:57 AM   #7
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
#!/usr/bin/perl -w

$date=`date`;

split(/\s+/, $date);

foreach (@_) {
     print  ++$n, " ", $_, "\n";
}
Quote:
$ ./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
version 5.008007
 
Old 07-20-2013, 03:39 AM   #8
bonnydeal
Member
 
Registered: Feb 2006
Posts: 47

Rep: Reputation: 29
It does not work for the OP or me.
 
Old 07-20-2013, 07:30 AM   #9
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
It's been taken out of later versions of Perl. 5.014 for instance needs an array explicitly set to the split() return values.
 
  


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] setting new variable to regex of another without changing the other variable in perl Eppo Programming 1 12-22-2011 02:35 PM
Perl Variable help abs_77 Programming 5 05-11-2009 09:45 AM
Perl alarm not working when reading for a socket , perl seems to be buggy alix123 Programming 1 10-05-2008 07:36 PM
perl @INC variable nodger Programming 3 07-10-2006 11:17 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu

All times are GMT -5. The time now is 05:29 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