LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-15-2004, 03:36 PM   #1
KneeLess
Member
 
Registered: May 2003
Distribution: Debian GNU/Linux 3.0 Sid, OpenBSD 3.5
Posts: 190

Rep: Reputation: 30
Perl: Terenary + Refs


Hello, I have the following questions. I want to traverse through two arrays, but I want the bigger one to be in the 'parent' loop if you will. This could possibly save 10 minutes on large lists of files. But, I wrote the following thinking I was very clever (which, as a matter of fact, am obviously not):
Code:
foreach((scalar(@befor) > scalar(@after)) ? \@befor : \@after)
{ 
  foreach((scalar(@befor) < scalar(@after)) ? \@befor : \@after)
  {
     #okay
  }
}
Now what this "should" do if I understand references in perl, is return a reference to the array in the foreach. This does not work, why? Then, on that defeat I tried adding an ampersand before the backslash on the references for the arrays. But this simply returns what looks like the memory address for the array, which is not what I want.

Am I completely wrong in my reasoning? I know their are easier ways of doing this , but I just want to know why this doesn't work.
 
Old 09-16-2004, 06:59 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,670

Rep: Reputation: 2487Reputation: 2487Reputation: 2487Reputation: 2487Reputation: 2487Reputation: 2487Reputation: 2487Reputation: 2487Reputation: 2487Reputation: 2487Reputation: 2487
1. This is an 'at' sign: @
2. This is an ampersand: '&'

If outer loop loops 10 times and inner loop loops 3 times, thats 10 x 3 = 30
if outer loop loops 3 times and inner loop loops 10 times, thats 3 x 10 = 30
ie multiplication is a commutative operation.

A ref is a scalar value. you need a list eg @array in the loop hdrs. If this is going to work (???) I'd say you'd need
foreach( @ ( (scalar(@befor) > scalar(@after)) ? \@befor : \@after) )
but i haven't tried it...
 
Old 09-16-2004, 10:38 AM   #3
KneeLess
Member
 
Registered: May 2003
Distribution: Debian GNU/Linux 3.0 Sid, OpenBSD 3.5
Posts: 190

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by chrism01
1. This is an 'at' sign: @
2. This is an ampersand: '&'
Thanks, I know what they are. I was saying I tried this also:
Code:
foreach((scalar(@befor) > scalar(@after)) ? &\@befor : &\@after)
Quote:
Originally posted by chrism01
A ref is a scalar value. you need a list eg @array in the loop hdrs. If this is going to work (???) I'd say you'd need
foreach( @ ( (scalar(@befor) > scalar(@after)) ? \@befor : \@after) )
but i haven't tried it...
No, that doesn't work, because using @ doesn't make it a list or an array.
Quote:
Originally posted by chrism01
If outer loop loops 10 times and inner loop loops 3 times, thats 10 x 3 = 30
if outer loop loops 3 times and inner loop loops 10 times, thats 3 x 10 = 30
ie multiplication is a commutative operation.
Yes, I'm familiar with that, but due to conditions inside the loop, the outer loop may stop commencing, and it'll save time. And the funniest thing, using it in it's normal context '@befor' works fine.

Last edited by KneeLess; 09-16-2004 at 10:51 AM.
 
Old 09-16-2004, 03:08 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,670

Rep: Reputation: 2487Reputation: 2487Reputation: 2487Reputation: 2487Reputation: 2487Reputation: 2487Reputation: 2487Reputation: 2487Reputation: 2487Reputation: 2487Reputation: 2487
Here's some good examples in The Perl Cookbook of iterating on array refs.
http://iis1.cps.unizar.es/Oreilly/pe...ok/ch04_06.htm
Also, in my experience, '&' in Perl denotes a subroutine.... although it's not needed in Perl5 unless you are talking about a ref to a sub.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
LQ REQ: Real newbie HOWTO's, refs, etc etc. unSpawn Linux - Security 200 01-06-2006 07:09 PM
Problem with perl module for w3c validator to work on my local Apache+PHP+perl instal tbamt Linux - Software 0 12-16-2004 05:37 PM
Yum error: .conflict between perl and perl-NDBM_File zepplin611 Red Hat 3 10-20-2004 10:22 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
chrooting apache v2 (php, ssl, perl support) ; perl configuration markus1982 Linux - Security 3 01-26-2003 06:15 PM

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

All times are GMT -5. The time now is 03:42 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration