Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Can't write values from sorted array of structs

by maschwarz (Initiate)
on Oct 28, 2020 at 16:51 UTC ( #11123269=perlquestion: print w/replies, xml ) Need Help??

maschwarz has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to sort and a comma-separated list and insert a line from another file. I created an array of structs as a test, but it wouldn't print outside of dumper, what am I doing wrong

use strict; use warnings; use Class::Struct; package Carrier_Info; struct Carrier_Info => { name => '$', abbrev => '$', }; my $aa="TH"; my @carriers; { #method 2 push @carriers, Carrier_Info->new(name => "Paul Lalli", abbrev =>" +PL"); push @carriers, Carrier_Info->new(name => "Travis Hervey", abbrev= +> $aa); my $xyz; $xyz = pop(@carriers); my $ppp = $xyz->{name}; print "which name is $ppp\n"; }

This prints either "Array(0x)", "Hash(0x)" or nothing. I'm expecting it to print "Travis Harvey". Once I understand using structs I can proceed with my real perl program Michael

Replies are listed 'Best First'.
Re: Can't write values from sorted array of structs
by hippo (Chancellor) on Oct 28, 2020 at 17:07 UTC

    I'm afraid that your code as supplied doesn't compile for me. Using Class::Struct 0.65 with perl v5.20.3 I see:

    syntax error at 11123269.pl line 11, near "struct Carrier_Info" Execution of 11123269.pl aborted due to compilation errors.

    This seems reasonable because the documentation suggests that the arguments to struct should be within brackets. Is this really the code you are running?


    Update: here's an SSCCE showing what I think you expect to be output.

    #!/usr/bin/env perl use strict; use warnings; package Carrier_Info; use Class::Struct; struct (Carrier_Info => { name => '$', abbrev => '$', }); my $aa = "TH"; my @carriers; push @carriers, Carrier_Info->new(name => "Paul Lalli", abbrev =>"PL") +; push @carriers, Carrier_Info->new(name => "Travis Hervey", abbrev=> $a +a); my $xyz = pop(@carriers); my $ppp = $xyz->name; print "which name is $ppp\n";

    I won't claim this is the neatest as I never use Class::Struct directly but hopefully it will serve as an illustration.


    🦛

      "This seems reasonable because the documentation suggests that the arguments to struct should be within brackets."

      The parentheses aren't important. OP puts use Class::Struct before the package statement, so the struct function is being imported into main instead of into Carrier_Info.

Re: Can't write values from sorted array of structs
by tobyink (Canon) on Oct 29, 2020 at 09:43 UTC

    This is your biggest issue:

    use Class::Struct; package Carrier_Info;

    You want:

    package Carrier_Info; use Class::Struct;

    Why? Because the struct "keyword" isn't a keyword in the sense of the Perl keyword API; it's not lexically scoped. It's a sub exported to a package, and by importing it before your package statement, you're exporting it outside your package (probably into main) instead of into your package.

    Better yet, leave the package Carrier_Info; part out entirely. It's not needed, and seems to suggest you think you need to define the Carrier_Info package yourself; the whole point of using Class::Struct is to allow it to define the Carrier_Info package for you!

    Lesser issue (because while it stops your code from working, it's not a syntax error), but still an issue:

    my $ppp = $xyz->{name};

    You want:

    my $ppp = $xyz->name;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11123269]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2020-11-30 14:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?