what is this data structure?

2004-01-16 Thread Jack Chen
Hi,

I don't know how to work with this data structure:

my @array = ((a =>  'appple'),
 (b =>  'tree'),
 (c =>  'chair'));

when I do:

print @array;

I get no output.

when I do:

use Data::Dumper;
print Dumper(@array);

I got the contents of it.

Thanks,

Jack



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: what is this data structure?

2004-01-16 Thread Randy W. Sims
On 1/16/2004 11:03 AM, Jack Chen wrote:

Hi,

I don't know how to work with this data structure:

my @array = ((a =>   'appple'),
 (b =>   'tree'),
 (c =>   'chair'));
when I do:

print @array;

I get no output.

when I do:

use Data::Dumper;
print Dumper(@array);
I got the contents of it.
Despite all of the punctuation, it is nothing more than an array. The
following is equivelant:
my @array = qw( a apple b tree c chair );

I find it more helpfull when using Data::Dumper to print a reference to
the variable I want to examine. I.e.
print Dumper([EMAIL PROTECTED]);

To me it makes the structure easier to interpret. I don't understand
though why you saw nothing for 'print @array', are you sure you ran the
code as above. I get:
print @array;
=> aappplebtreecchair
print "@array";
=> a appple b tree c chair
Regards,
Randy.




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: what is this data structure?

2004-01-16 Thread Jack Chen
Hi Randy,

Thanks for reply!

It is strage. When I tried the script on another machine, it works the way
you described. Anything wrong about my perl or my OS (Linux 8)?

Jack

On Fri, 16 Jan 2004, Randy W. Sims wrote:

> On 1/16/2004 11:03 AM, Jack Chen wrote:
> 
> > Hi,
> > 
> > I don't know how to work with this data structure:
> > 
> > my @array = ((a =>  'appple'),
> >  (b =>  'tree'),
> >  (c =>  'chair'));
> > 
> > when I do:
> > 
> > print @array;
> > 
> > I get no output.
> > 
> > when I do:
> > 
> > use Data::Dumper;
> > print Dumper(@array);
> > 
> > I got the contents of it.
> 
> Despite all of the punctuation, it is nothing more than an array. The
> following is equivelant:
> 
> my @array = qw( a apple b tree c chair );
> 
> I find it more helpfull when using Data::Dumper to print a reference to
> the variable I want to examine. I.e.
> 
> print Dumper([EMAIL PROTECTED]);
> 
> To me it makes the structure easier to interpret. I don't understand
> though why you saw nothing for 'print @array', are you sure you ran the
> code as above. I get:
> 
> print @array;
> => aappplebtreecchair
> 
> print "@array";
> => a appple b tree c chair
> 
> Regards,
> Randy.
> 
> 
> 
> 
> 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: what is this data structure?

2004-01-16 Thread Charles K. Clarkson
Jack Chen <[EMAIL PROTECTED]> wrote:
: 
: I don't know how to work with this data structure:
: 
: my @array = ((a   =>  'appple'),
:  (b   =>  'tree'),
:(c =>  'chair'));
: 
: when I do:
: 
: print @array;
: 
: I get no output.

It printed okay for me. Perhaps something else is
wrong. Show us everything.

aappplebtreecchair


HTH,

Charles K. Clarkson
-- 
Head Bottle Washer,
Clarkson Energy Homes, Inc.
Mobile Home Specialists
254 968-8328


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: what is this data structure?

2004-01-17 Thread Dan Anderson
On Fri, 2004-01-16 at 11:03, Jack Chen wrote:
> Hi,
> 
> I don't know how to work with this data structure:
> 
> my @array = ((a   =>  'appple'),
>  (b   =>  'tree'),
>(c =>  'chair'));

That "array" is an array of hashes.

-Dan


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: what is this data structure?

2004-01-17 Thread James Edward Gray II
On Jan 17, 2004, at 2:02 PM, Dan Anderson wrote:

On Fri, 2004-01-16 at 11:03, Jack Chen wrote:
Hi,

I don't know how to work with this data structure:

my @array = ((a =>   'appple'),
 (b =>   'tree'),
 (c =>   'chair'));
That "array" is an array of hashes.
It was probably meant to be, but it is not as written.  Look again.

James

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: what is this data structure?

2004-01-17 Thread Dan Anderson
On Sat, 2004-01-17 at 15:12, James Edward Gray II wrote:
> On Jan 17, 2004, at 2:02 PM, Dan Anderson wrote:
> 
> > On Fri, 2004-01-16 at 11:03, Jack Chen wrote:
> >> Hi,
> >>
> >> I don't know how to work with this data structure:
> >>
> >> my @array = ((a=>  'appple'),
> >>  (b=>  'tree'),
> >> (c =>  'chair'));
> >
> > That "array" is an array of hashes.
> 
> It was probably meant to be, but it is not as written.  Look again.

Ahhh, you're right.  Correct me if I'm wrong, but the arrays expand out,
so although (a -> 'apple') is a hash and (b => 'tree') is a hash, it
actually contains the hash:

(
  a => 'apple',
  b => 'tree',
  c => 'chair',
)

Correct?

-Dan


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: what is this data structure?

2004-01-17 Thread Randy W. Sims
On 1/17/2004 3:16 PM, Dan Anderson wrote:

On Sat, 2004-01-17 at 15:12, James Edward Gray II wrote:

On Jan 17, 2004, at 2:02 PM, Dan Anderson wrote:


On Fri, 2004-01-16 at 11:03, Jack Chen wrote:

Hi,

I don't know how to work with this data structure:

my @array = ((a =>   'appple'),
(b  =>   'tree'),
 (c =>   'chair'));
That "array" is an array of hashes.
It was probably meant to be, but it is not as written.  Look again.


Ahhh, you're right.  Correct me if I'm wrong, but the arrays expand out,
so although (a -> 'apple') is a hash and (b => 'tree') is a hash, it
actually contains the hash:
(
  a => 'apple',
  b => 'tree',
  c => 'chair',
)
Correct?
Yes. One thing that may help is to remember that => is just a fancy 
comma and the parentheses are only for grouping-They don't create any 
structure or change the type of the thing they're grouping.



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: what is this data structure?

2004-01-17 Thread James Edward Gray II
On Jan 17, 2004, at 2:16 PM, Dan Anderson wrote:

On Sat, 2004-01-17 at 15:12, James Edward Gray II wrote:
On Jan 17, 2004, at 2:02 PM, Dan Anderson wrote:

On Fri, 2004-01-16 at 11:03, Jack Chen wrote:
Hi,

I don't know how to work with this data structure:

my @array = ((a =>   'appple'),
 (b =>   'tree'),
 (c =>   'chair'));
That "array" is an array of hashes.
It was probably meant to be, but it is not as written.  Look again.
Ahhh, you're right.  Correct me if I'm wrong, but the arrays expand 
out,
so although (a -> 'apple') is a hash and (b => 'tree') is a hash, it
actually contains the hash:
You're terminology is a little confusing, but I think you have the 
right idea.

('a', 'apple')  # this is a list, not an array or a hash
(a => 'apple')   # this is the exact same list
(a => 'apple', b => 'tree')		# this is just a bigger list
# and because Perl flattens all nested lists...
((a => 'apple'), (b => 'tree'))	# this is the same as the above, just 
one big list

my @array = ((a => 'apple'), (b => 'tree'));		# makes a four element 
array
my %hash = ((a => 'apple'), (b => 'tree'));		# makes a two key hash

Hope that clears things up.

James

(
  a => 'apple',
  b => 'tree',
  c => 'chair',
)
Correct?

-Dan




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: what is this data structure?

2004-01-17 Thread Dan Anderson
> You're terminology is a little confusing, but I think you have the 
> right idea.

For the benefit of the original poster:

The => symbol is usually used to signify a hash and automatically quote
the key to the left of it.  I.e.:

my %hash1 = (
 foo => 'bar',
 bar => 'baz',
 baz => 'foo',
);

However a hash is just an even valued list, so %hash1 is the same as
%hash2:

my %hash2 = ('foo', 'bar', 'bar', 'baz', 'baz', 'foo');

However, a lot of times you'll see a hash in a hash reference, i.e.:

my $hashref = { foo => 'bar', bar => 'baz', baz => 'foo', };

So that instead of accessing elements by $hash1{foo} you'd access them
by $hash1->{foo}.

I think I've successfully improved, right?

-Dan


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: what is this data structure?

2004-02-05 Thread R. Joseph Newton
Dan Anderson wrote:

> However a hash is just an even valued list, so %hash1 is the same as
> %hash2:
> So that instead of accessing elements by $hash1{foo} you'd access them
> by $hash1->{foo}.
>
> I think I've successfully improved, right?

Yes.  You're closer, but still a little off.:

> However a hash is just an even valued list, so %hash1 is the same as
> %hash2:

A hash is a heckuva lot more than an even valued list.  Hashes take  lists
with even number of values as static initializers.  They also serialize
themselves as similar lists for transfer through interfaces.  When intact,
though, they use storage and retrieval algorithms that have nothing to do
with sinple array structures.  It is the transfer process that squeezes the
magic out of them.

 This is why we use references.  By leaving the hash in place, and passing
a variable by which the intact structure can be accessed, we harness its
full power, which is truly immense.

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]