newbie help with some help needed with a data array

Discussion in 'Perl Misc' started by LeTubs, Jan 25, 2004.

  1. LeTubs

    LeTubs Guest

    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
     
    LeTubs, Jan 25, 2004
    #1
    1. Advertising

  2. LeTubs

    Bob Walton Guest

    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.
    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
     
    Bob Walton, Jan 25, 2004
    #2
    1. Advertising

  3. LeTubs

    Sam Holden Guest

    On Sun, 25 Jan 2004 01:27:12 GMT,
    Bob Walton <> wrote:
    > 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


    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.

    >> #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){


    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.)

    --
    Sam Holden
     
    Sam Holden, Jan 25, 2004
    #3
  4. "LeTubs" <> wrote in
    news:k_DQb.28328$:

    > 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
    --
    A. Sinan Unur
    (reverse each component for email address)
     
    A. Sinan Unur, Jan 25, 2004
    #4
  5. LeTubs

    Matt Garrish Guest

    "LeTubs" <> wrote in message
    news:k_DQb.28328$...
    > 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";
    }
     
    Matt Garrish, Jan 25, 2004
    #5
  6. LeTubs

    Matt Garrish Guest

    "Matt Garrish" <> wrote in message
    news:qjFQb.21871$...
    >
    > "LeTubs" <> wrote in message
    > news:k_DQb.28328$...
    >
    > > #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
     
    Matt Garrish, Jan 25, 2004
    #6
  7. Bob Walton <> wrote:
    > LeTubs wrote:


    >> 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){



    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 ) {


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jan 25, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Serendipity

    Some Help needed

    Serendipity, Feb 10, 2006, in forum: Java
    Replies:
    0
    Views:
    339
    Serendipity
    Feb 10, 2006
  2. Vibhajha
    Replies:
    12
    Views:
    613
    Howard
    Oct 25, 2004
  3. Thomas Nick
    Replies:
    0
    Views:
    1,889
    Thomas Nick
    Jun 13, 2005
  4. sigi
    Replies:
    4
    Views:
    535
  5. Ryan Macy

    Ideas needed & help needed!

    Ryan Macy, Jul 19, 2006, in forum: Ruby
    Replies:
    2
    Views:
    518
    Ryan Macy
    Jul 19, 2006
Loading...

Share This Page