Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 05-16-2008, 01:08 PM   #1
LQ Newbie
Registered: Jan 2008
Distribution: Ubuntu Hardy Heron
Posts: 18

Rep: Reputation: 0
[perl] copying an array element into another array

hi there.

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

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?


Old 05-16-2008, 06:56 PM   #2
HCL Maintainer
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 76
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.,
my @load = map $cpu->load($_), 0, 1, 2;
Or, as a hash,
my %load_by_minute;
@load_by_minute{1, 5, 10} = map $cpu->load($_), 0, 1, 2;
Old 05-17-2008, 01:47 AM   #3
LQ Newbie
Registered: Jan 2008
Distribution: Ubuntu Hardy Heron
Posts: 18

Original Poster
Rep: Reputation: 0
Thanks alot, it works perfectly now


array, copy, perl

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

All times are GMT -5. The time now is 06:19 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration