LinuxQuestions.org
Visit Jeremy's Blog.
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

Tags used in this thread
Popular LQ Tags , ,

Reply
 
Thread Tools
Old 05-16-2008, 02:08 PM   #1
s0l1dsnak3123
LQ Newbie
 
Registered: Jan 2008
Distribution: Ubuntu Hardy Heron
Posts: 18
Thanked: 0
[perl] copying an array element into another array


[Log in to get rid of this advertisement]
hi there.

I want to copy all array elements from $cpu->load[] to @load. I have tried the following without any success:

Code:
my $info = Sys::Info->new;
my $cpu  = $info->device( 'CPU' );

my @load;
foreach ($cpu->load) {
push(@load, $_);
print $_ . "\n";
}
Is there another way for me to do this?

thanks,

s0l1dsnak3123
s0l1dsnak3123 is offline  
Tag This Post , ,
Reply With Quote
Old 05-16-2008, 07:56 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,434
Thanked: 24
Your question does not make sense. $cpu->load is a subroutine, not an array. The subroutine always returns a scalar. The value returned depends on the subroutine’s first parameter. According to this, the valid parameters are 0, 1, or 2 for the load in the last 1, 5, or 10 minute interval respectively.

So if you want to get all the values, you can map the specified values to the subroutine with said values as a parameter. E.g.,
Code:
my @load = map $cpu->load($_), 0, 1, 2;
Or, as a hash,
Code:
my %load_by_minute;
@load_by_minute{1, 5, 10} = map $cpu->load($_), 0, 1, 2;
osor is offline     Reply With Quote
Old 05-17-2008, 02:47 AM   #3
s0l1dsnak3123
LQ Newbie
 
Registered: Jan 2008
Distribution: Ubuntu Hardy Heron
Posts: 18
Thanked: 0

Original Poster
Thanks alot, it works perfectly now
s0l1dsnak3123 is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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
Find whether an element exists in an array. Sherlock Programming 8 06-21-2007 08:07 AM
index of an element in the array ? thelonius Programming 1 09-24-2005 01:41 PM
number of element in a array of pointer? os2 Programming 7 03-09-2005 12:50 PM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 03:24 PM
C++ Six Element Array petercool Programming 2 08-20-2003 12:08 PM


All times are GMT -5. The time now is 11:37 AM.

Main Menu
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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration