Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Array in a hash question?

Reply
Thread Tools

Array in a hash question?

 
 
Jamie Smyth
Guest
Posts: n/a
 
      05-29-2004
I have a hash defined as,

my %data = (
'wn_l' => $wn_l, 'wn_u' => $wn_u, 'wn_d' => $wn_d,
'npts' => $#spc+1, 'spc' => [@spc], 'label' => $label,
);

and I would like to extract the data in the @spc array. I tried,

my @spc = %data{'spc'};

and

my $spc = %data{'spc'};

but this results in the array being created directly in @spc[0]. I
ended up using:

my $d = $data{'spc'};
my @data = ();
push @data, @$d;

which gives me a 1dimensional array @data that I can use... the
problem is that I don't really know why this works and it looks a
little weird. Can someone explain?

Thanks.
 
Reply With Quote
 
 
 
 
Matt Garrish
Guest
Posts: n/a
 
      05-29-2004

"Jamie Smyth" <> wrote in message
news: om...
> I have a hash defined as,
>
> my %data = (
> 'wn_l' => $wn_l, 'wn_u' => $wn_u, 'wn_d' => $wn_d,
> 'npts' => $#spc+1, 'spc' => [@spc], 'label' => $label,
> );
>
> and I would like to extract the data in the @spc array. I tried,
>
> my @spc = %data{'spc'};
>


The value for key spc is a reference to an array (hash values are always
scalars). In order to access the array, you need to dereference the
reference:

my @array = @{ $data{'spc'} };

Matt


 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      05-30-2004
Jamie Smyth <> wrote:
> I have a hash defined as,
>
> my %data = (
> 'wn_l' => $wn_l, 'wn_u' => $wn_u, 'wn_d' => $wn_d,
> 'npts' => $#spc+1, 'spc' => [@spc], 'label' => $label,
> );
>
> and I would like to extract the data in the @spc array.



Apply "Use Rule 1" from:

perldoc perlreftut


1) pretend is is a plain array

my @spc = @data;

2) replace the _name_ with a block ...

my @spc = @{ };

3) ... that returns a reference to an array

my @spc = @{ $data{spc} };



--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      05-30-2004
Matt Garrish wrote:
>
> "Jamie Smyth" <> wrote in message
> news: om...
> > I have a hash defined as,
> >
> > my %data = (
> > 'wn_l' => $wn_l, 'wn_u' => $wn_u, 'wn_d' => $wn_d,
> > 'npts' => $#spc+1, 'spc' => [@spc], 'label' => $label,
> > );
> >
> > and I would like to extract the data in the @spc array. I tried,
> >
> > my @spc = %data{'spc'};

>
> The value for key spc is a reference to an array (hash values are always
> scalars). In order to access the array, you need to dereference the
> reference:
>
> my @array = @{ $data{'spc'} };


Also, the 'npts' entry isn't required as the array in scalar context
will return the number of elements:

my $npts = @{ $data{ spc } };


John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
 
 
Reply

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Benchmark segfault [Was: Array#inject to create a hash versus Hash[*array.collect{}.flatten] ] Michal Suchanek Ruby 6 06-13-2007 04:40 AM
Array#inject to create a hash versus Hash[*array.collect{}.flatten] -- Speed, segfault Anthony Martinez Ruby 4 06-11-2007 08:16 AM
Sort by hash vaule, an array of hash references fahdsultan@gmail.com Perl Misc 11 10-10-2005 09:35 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57