Perl - Parentheses & Context confusion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl - Parentheses & Context confusion
# 1  
Old 11-26-2009
Perl - Parentheses & Context confusion

Hi Expert,

Could you please explain why below two perl code get different result?

Thanks a lot.

Code:
sub test{
	return (2,3,4,5,6,3,4,50);
}

($a,$b)=(test)[1,4]; # 3,6

($a,$b)=test[1,4]; # 2,3

# 2  
Old 11-26-2009
Code:
($a,$b)=(test)[1,4]; # 3,6
In the above statement it calls the function test, and takes the
return value of test as an array and return its 1st and 4th indexes.
so you are getting 3 and 6

($a,$b)=test[1,4]; # 2,3
In the above statement, it calls the function and returns the
array. since you assigned the return values to two scalar
you are getting first two index of array.

Here there is no meaning of giving [1,4].

You just try the following you will understand:
($a,$b)= test => will give 2 and 3
($a,$b)= test[4,5] => will give 2 and 3
It will give only 0 and 1st index irrespective of index 
you give here.

# 3  
Old 11-26-2009
Thanks so much.

Now i fully understand.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Questions related to if in awk context and if without awk context

I wrote this code, questions follow #! /bin/bash -f # Purpose - to show how if syntax is used within an awk clear; ls -l; echo "This will print out the first two columns of the inputted file in this directory"; echo "Enter filename found in this directory"; read input; ... (11 Replies)
Discussion started by: Seth
11 Replies

2. Shell Programming and Scripting

Wildcarding in a Perl @ARGV Context?

Hello folks! While "sedding" about again, I ran into this little conundrum du jour:#!/usr/bin/perl use strict; use warnings; use diagnostics; @ARGV = ('./afile.dat', './*.txt'); $^I = ''; while (<>) { s/Twinkies/Dinner/g; print; }When run, perl complains,...but, of... (2 Replies)
Discussion started by: LinQ
2 Replies

3. Shell Programming and Scripting

Wildcarding in a Perl @ARGV Context?

Hello folks! While "sedding" about again, I ran into this little conundrum du jour:#!/usr/bin/perl use strict; use warnings; use diagnostics; @ARGV = ('./afile.dat', './*.txt'); $^I = ''; while (<>) { s/Twinkies/Dinner/g; print; }When run, perl complains,...but, of... (1 Reply)
Discussion started by: LinQ
1 Replies

4. Shell Programming and Scripting

Confusion with "su -c" and quotes, user context switching?

Trying to execute commands for different Unix user with that user's environment variable context without fully switching as that user using sudo && su capabilities. Hoping this would help with security and not having to waste time switching between 10 different app users on same server. I do... (6 Replies)
Discussion started by: kchinnam
6 Replies

5. Shell Programming and Scripting

Opening Child Shell & Executing a script in the same context

Hi, Is the below possible (SHELL = tcsh)? -- I want to write an 'alias' something like this - alias set_my_work "setenv SOME_VAR;tcsh -i;source work_script.cshrc" The intention is to run this alias and enter a child shell, at the same time ensuring that the work_script.cshrc is source-ed.... (0 Replies)
Discussion started by: mishra.a.c
0 Replies

6. Solaris

Confusion in vxfs & ufs

Hi All, I have rootdg encapsulated within Veritas Volume Manager. /# vxprint -vg rootdg TY NAME ASSOC KSTATE LENGTH PLOFFS STATE TUTIL0 PUTIL0 v rootvol root ENABLED 24585216 - ACTIVE - - v swapvol swap ENABLED 20484288 -... (3 Replies)
Discussion started by: solaris_1977
3 Replies

7. Shell Programming and Scripting

Find & Replace string in multiple files & folders using perl

find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g' this is simple logic to find and replace in multiple files & folders Hope this helps. Thanks Zaheer (0 Replies)
Discussion started by: Zaheer.mic
0 Replies

8. Shell Programming and Scripting

Perl regex help - matching parentheses

Let's say I'm trying to match potentially multiple sets of parentheses. Is there a way in a regular expression to force a match of closing parentheses specifically in the number of the opening parentheses? For example, if the string is "((foo bar))", I want to be able to say "match any number of... (7 Replies)
Discussion started by: cvp
7 Replies

9. Shell Programming and Scripting

Confusion about 1>&2 and 2>&1

Can anybody tell me the difference between 1>&2 and 2>&1? (1 Reply)
Discussion started by: navi
1 Replies

10. Shell Programming and Scripting

Parentheses in perl find/replace

I'm trying to use the following command to do a batch find and replace in all commonly named files through a file hierarchy find . -name 'file' |xargs perl -pi -e 's/find/replace/g' which works fine except for a substitution involving parenthesis. As a specific example I'm trying to sub... (3 Replies)
Discussion started by: Jeffish
3 Replies
Login or Register to Ask a Question