newbie help with some help needed with a data array

L

LeTubs

Hi
What I'm trying do is to have a record structure and then create an array
of those structures and then being able to access those structures via
for loop..... sounds simple, well unfortunately I want do this in perl and
being sort of new to it I'm sort of coming stuck( or continuing to think in
C ). I have written a simple little test program that I'm seeing whats is
going on and trying to figure whats going wrong, and would a appreciate
some expert advice !

Thanks
David

#!/usr/bin/perl
use Class::Struct;

#create a simple record structure
struct Wibble =>{
name => '$',
w => '$',
};

#create a blank array for storage....
@some_array = ();

# create the new member
my $p = Wibble->new();
$p->name("David");
$p->w("w");

# store the new member ( is this correct way to do this ? )
push( @some_array, $p );

# create another member.....
my $p = Wibble->new();
$p->name("William");
$p->w("x");

# store it again
push( @some_array, $p );

#now print out the results....
for( $i = 0; $i < @some_array; $i++ ){
print " $i : $some_array[$i].name, $some_array[$i].w";
}

# Ideal Ouput
1 : Davidw
2 : Williamx
 
B

Bob Walton

LeTubs wrote:

....
What I'm trying do is to have a record structure and then create an array
of those structures and then being able to access those structures via
for loop..... sounds simple, well unfortunately I want do this in perl and
being sort of new to it I'm sort of coming stuck( or continuing to think in
C ). I have written a simple little test program that I'm seeing whats is
going on and trying to figure whats going wrong, and would a appreciate
some expert advice ! ....


David

#!/usr/bin/perl

use strict; #always during development
use warnings; #always during development -- let Perl help you

use Class::Struct;

#create a simple record structure
struct Wibble =>{
name => '$',
w => '$',
};

#create a blank array for storage....
@some_array = ();
my^



# create the new member
my $p = Wibble->new();
$p->name("David");
$p->w("w");

# store the new member ( is this correct way to do this ? )
push( @some_array, $p );

# create another member.....
my $p = Wibble->new();

---^^---delete this my, as it is a duplicate in this scope

$p->name("William");
$p->w("x");

# store it again
push( @some_array, $p );

#now print out the results....
for( $i = 0; $i < @some_array; $i++ ){


That is a very C-ish for loop. In Perl, it would be better written as:

for(@some_array){

print " $i : $some_array[$i].name, $some_array[$i].w";

huh?------^----------------------^^^^^-----------------^^^

The syntax would be $some_array[$i]->name, plus you put it in the middle
of a "-quoted string, which means that interpolation, not evaluation, is
performed, which means the -> wouldn't be executed, but rather
substituted. And you forgot to print a newline. See below for a
modified version of your program which works.

Note that you can:

use Data::Dumper;

to print out things like @some_array -- it will show you exactly what
you have with no fuss, and then you can go from there.

}

# Ideal Ouput
1 : Davidw
2 : Williamx

#!/usr/bin/perl
use strict;
use warnings; #let Perl help you!!!
use Class::Struct;

#create a simple record structure
struct Wibble =>{
name => '$',
w => '$',
};

#create a blank array for storage....
my @some_array = ();

# create the new member
my $p = Wibble->new();
$p->name("David");
$p->w("w");

# store the new member ( is this correct way to do this ? )
push( @some_array, $p );

# create another member.....
$p = Wibble->new();
$p->name("William");
$p->w("x");

# store it again
push( @some_array, $p );

#now print out the results....
my $i=0;
for(@some_array){
print ++$i.": ".$_->name.", ".$_->w."\n";
}

HTH.
 
S

Sam Holden

LeTubs wrote:

...

use strict; #always during development

Always full stop.
use warnings; #always during development -- let Perl help you

Disabling warnings after development is reasonable - though I
don't bother... Some users might be scared of the messages it
may produce due to thing slike unexpected input.
That is a very C-ish for loop. In Perl, it would be better written as:

for(@some_array){

Since $i is used in the loop body, how do you propose to produce
the same results with that?

for my $i (0...$#some_array) {

Would be the "more perlish" equivalent.
#now print out the results....
my $i=0;
for(@some_array){
print ++$i.": ".$_->name.", ".$_->w."\n";
}

Oh, that's how you propose to do it. You've successfully
introduced an extra variable whose scope it greater than
the scope of the loop. I'd prefer the C style code to that,
and I'd prefer the ... style I showed above to that.

(Of course that variable existed in the original as well, but it
could have its scope reduced via my easily enough.)
 
A

A. Sinan Unur

Hi
What I'm trying do is to have a record structure and then create an
array of those structures and then being able to access those
structures via for loop..... sounds simple, well unfortunately I want
do this in perl and being sort of new to it I'm sort of coming stuck(
or continuing to think in C ). I have written a simple little test
program that I'm seeing whats is going on and trying to figure whats
going wrong, and would a appreciate some expert advice ! ....

#!/usr/bin/perl

You are missing:

use strict;
use warnings;

Those lines would have helped you see, among other things:

"my" variable $p masks earlier declaration in same scope at C:\Home\t.pl
line 26.
use Class::Struct;

#create a simple record structure
struct Wibble =>{
name => '$',
w => '$',
};

#create a blank array for storage....
@some_array = ();

This is not really necessary.
#now print out the results....
for( $i = 0; $i < @some_array; $i++ ){
print " $i : $some_array[$i].name, $some_array[$i].w";
}

# Ideal Ouput
1 : Davidw
2 : Williamx

I do not see any way the loop above could produce that output for many
reasons such as the fact that $i only takes on the values 0 and 1. Also,
you have a comma and a space between the two fields in the print statement.

I have made a few modifications to your script. See below:

#! /usr/bin/perl

use strict;
use warnings;

use Class::Struct;

struct Wibble => {
name => '$',
w => '$',
};

my @some_array;

push @some_array,
Wibble->new(name => 'David', w => 'w'),
Wibble->new(name => 'William', w => 'x');

# Do you really need to display the index?
for (@some_array) {
print $_->name, ' ', $_->w, "\n";
}

# if so, you can use this:
for (0 .. $#some_array) {
print "$_ :", $some_array[$_]->name, ' ', $some_array[$_]->w, "\n";
}

__END__

Sinan
 
M

Matt Garrish

LeTubs said:
Hi
What I'm trying do is to have a record structure and then create an array
of those structures and then being able to access those structures via
for loop..... sounds simple, well unfortunately I want do this in perl and
being sort of new to it I'm sort of coming stuck( or continuing to think in
C ). I have written a simple little test program that I'm seeing whats is
going on and trying to figure whats going wrong, and would a appreciate
some expert advice !

I'll start by saying that you should really go back to the documentation for
Class::Struct and take another look at the examples at the top. No point in
guessing when it's all laid out for you there.
#!/usr/bin/perl
use Class::Struct;

use strict;
use warnings;

#create a simple record structure
struct Wibble =>{
name => '$',
w => '$',
};

That's not how to create a new structure. As per the module documentation,
it should be:

struct ( Wibble => [
name => '$',
w => '$' ] );

#create a blank array for storage....
@some_array = ();

Better written as:

my @some_array;
# create the new member
my $p = Wibble->new();
$p->name("David");
$p->w("w");

# store the new member ( is this correct way to do this ? )
push( @some_array, $p );

Think pointers. You want to create an array of references to the struct:

push @some_array, \$p;
# create another member.....
my $p = Wibble->new();
$p->name("William");
$p->w("x");

If you want to reuse $p, undef it. Redeclaring the variable will cause an
error under strictures and warnings.
# store it again
push( @some_array, $p );

#now print out the results....
for( $i = 0; $i < @some_array; $i++ ){
print " $i : $some_array[$i].name, $some_array[$i].w";
}

Ugly C-style loops make my head hurt... : )

for my $i (0..$#some_array) {
print "$i : " . ${$some_array[$i]}->name . ' ' . ${$some_array[$i]}->w .
"\n";
}

To access the values above, you need to dereference the hash ref you stored
in the array (${$some_array[$i]}). I'll post a complete working script
below.

Matt



use strict;
use warnings;
use Class::Struct;

struct ( Wibble => [
name => '$',
w => '$' ] );

my @some_array;

my $p = Wibble->new();
$p->name('David');
$p->w('w');

push @some_array, \$p;

my $q = new Wibble;
$q->name('William');
$q->w('x');

push @some_array, \$q;

for my $i (0..$#some_array) {
print "$i : " . ${$some_array[$i]}->name . ' ' . ${$some_array[$i]}->w .
"\n";
}
 
M

Matt Garrish

Matt Garrish said:
#create a simple record structure
struct Wibble =>{
name => '$',
w => '$',
};

That's not how to create a new structure. As per the module documentation,
it should be:

struct ( Wibble => [
name => '$',
w => '$' ] );

Sheesh, this was not one of my better posts... : (

I think I thunk I saw an = instead of =>
Think pointers. You want to create an array of references to the struct:

Or you can just push the struct right onto the array and avoid dereferencing
it later. I really wasn't looking at the bigger picture when I wrote this
(you got me thinking in C...).

Matt
 
T

Tad McClellan

Bob Walton said:
LeTubs wrote:


That is a very C-ish for loop. In Perl, it would be better written as:

for(@some_array){


That does not do the same thing.

The first loops over the indexes of @some_array.

The second loops over the _values_ of @some_array.

So, in Perl, it would be better written as:

for my $i ( 0 .. $#some_array ) {
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top