LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-20-2012, 11:37 PM   #1
zahheb
LQ Newbie
 
Registered: Aug 2012
Posts: 29

Rep: Reputation: Disabled
perl


sub int_all {
my @retlist = @_; # make safe copy for return
for my $n (@retlist) { $n = int($n) }
return @retlist;
}


friends i am not able to understand the meaning of the


for my $n (@retlist) { $n = int($n) }

please help
 
Old 08-21-2012, 12:14 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Neither do I ... it iterates over all array elements, sticking
each as it passes into the scalar $n, puts an integer version
of the value back into it, and overwrites it on each run w/o
having done anything w/ the new value.


In other words: apart from wasting a few CPU cycles it does nothing.


Cheers,
Tink
 
Old 08-21-2012, 04:28 AM   #3
gregAstley
LQ Newbie
 
Registered: Aug 2012
Distribution: ubuntu 11.10
Posts: 27

Rep: Reputation: 4
Quote:
Originally Posted by zahheb View Post
sub int_all {
my @retlist = @_; # make safe copy for return
for my $n (@retlist) { $n = int($n) }
return @retlist;
}


friends i am not able to understand the meaning of the


for my $n (@retlist) { $n = int($n) }

please help
Mu knowledge of Perl isn't so great but...
I tested this and (assuming an integer cast makes sense) it is converting whatever happens to be in @retlist to ints (the $ns are handles on the actual array values and so whats actually happening is something like $retlist[some index] = int($retlist[some index])). Try this bit of code to see:
Code:
#!/usr/bin/perl
use strict;
use warnings;

my @array = (1.4, 3.167, 1.5);
for my $n(@array)
{
    print "\n\$n was: $n";
    $n = int($n);
    print "\n\$n is now: $n"
}
result:

$n was: 1.4
$n is now: 1
$n was: 3.167
$n is now: 3
$n was: 1.5
$n is now: 1

Last edited by gregAstley; 08-21-2012 at 04:32 AM.
 
2 members found this post helpful.
Old 08-21-2012, 04:59 AM   #4
zahheb
LQ Newbie
 
Registered: Aug 2012
Posts: 29

Original Poster
Rep: Reputation: Disabled
so can we also write it as

for each $n (@array) {...}

instead of for $n (@array) {....}
 
Old 08-21-2012, 05:13 AM   #5
gregAstley
LQ Newbie
 
Registered: Aug 2012
Distribution: ubuntu 11.10
Posts: 27

Rep: Reputation: 4
Quote:
Originally Posted by zahheb View Post
so can we also write it as

for each $n (@array) {...}

instead of for $n (@array) {....}
not quite...change "for each" to "foreach" (never used it yet but "each" is a key word used in a different context) you might also want to fling a "my" infront of "$n"

Last edited by gregAstley; 08-21-2012 at 05:18 AM.
 
Old 08-21-2012, 05:48 AM   #6
zahheb
LQ Newbie
 
Registered: Aug 2012
Posts: 29

Original Poster
Rep: Reputation: Disabled
yes foreach.......

by the way ,each is also used to traverse through the elements of an array
 
Old 08-21-2012, 06:01 AM   #7
gregAstley
LQ Newbie
 
Registered: Aug 2012
Distribution: ubuntu 11.10
Posts: 27

Rep: Reputation: 4
Quote:
Originally Posted by zahheb View Post
yes foreach.......

by the way ,each is also used to traverse through the elements of an array
Thanks for letting me know...we've both learnt something today :]
 
Old 08-21-2012, 06:35 AM   #8
zahheb
LQ Newbie
 
Registered: Aug 2012
Posts: 29

Original Poster
Rep: Reputation: Disabled
thanks to u too
 
Old 08-21-2012, 04:26 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by gregAstley View Post
Mu knowledge of Perl isn't so great but...
I tested this and (assuming an integer cast makes sense) it is converting whatever happens to be in @retlist to ints (the $ns are handles on the actual array values and so whats actually happening is something like $retlist[some index] = int($retlist[some index])). Try this bit of code to see:
Code:
#!/usr/bin/perl
use strict;
use warnings;

my @array = (1.4, 3.167, 1.5);
for my $n(@array)
{
    print "\n\$n was: $n";
    $n = int($n);
    print "\n\$n is now: $n"
}
result:

$n was: 1.4
$n is now: 1
$n was: 3.167
$n is now: 3
$n was: 1.5
$n is now: 1
I learnt something to; I didn't realise that the loop variable is actually
a reference to the actual array elements.


Cheers,
Tink
 
  


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
install vim via yum thinks perl is required - I build perl 5.14.2 from source rubanek Linux - General 4 05-02-2012 06:42 PM
Print output of a script to screen using Perl/Multiple installation of Perl Modules metallica1973 Linux - General 1 02-17-2011 05:59 PM
Trying to find libmail-mbox-messageparser-perl and libmailtools-perl for Fedora 11 almac58 Linux - Software 4 11-18-2009 03:17 AM
LXer: Installing Eclipse, the Epic Perl plugin and my first Perl GUI program LXer Syndicated Linux News 0 05-08-2009 06:41 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:03 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
Open Source Consulting | Domain Registration