Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

list of lists?

by guthrie (Novice)
on Jul 29, 2021 at 04:04 UTC ( [id://11135457]=perlquestion: print w/replies, xml ) Need Help??

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

beginner confusion... I had a list of values that i iterated over:
my @zips=("52544", "52557", "50428", "50219"); foreach $zip (@zips) { ... }
Now I want more information for each item, so I have this:
my @sites=( ("50049", "city1", "url1"), ("52556", "city2", "url2"), ("50219", "city3", "url3") ) foreach (my $zip, my $name, $my $url) { ... }
which fails. I have tried multiple permutations, but cannot seem to get the iteration over tuples from an array of tuples right. TIA.

Replies are listed 'Best First'.
Re: list of lists?
by Fletch (Bishop) on Jul 29, 2021 at 04:12 UTC

    Your extra parens don't do anything (as far as preserving structure) and you wind up with everything flattened out in one big array. You need a list of arrayrefs to initialize your array in order to keep your sub-lists distinct. See perllol and perldsc as well as perlreftut if you need the basics on references.

    my @sites = ( [ "blah", "city", "url1" ], ... ); for my $row (@sites) { my( $zip, $name, $url ) = @{ $row }; ...; }

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: list of lists?
by kcott (Archbishop) on Jul 29, 2021 at 05:21 UTC

    G'day guthrie,

    As an alternative, consider an HoH (Hash of Hashes) instead of an AoA (Array of Arrays). If you need to add even more information at some later point, this may be a more flexible solution. Here's a quick-and-dirty example:

    #!/usr/bin/env perl use strict; use warnings; my %site_info = ( 50049 => { city => 'city1', url => 'url1', }, 50219 => { city => 'city2', url => 'url2', }, ); my $fmt = "ZIP: %s\n\tCity: %s\n\tURL: %s\n"; for my $zip (keys %site_info) { printf $fmt, $zip, @{$site_info{$zip}}{qw{city url}}; }

    Output:

    ZIP: 50219 City: city2 URL: url2 ZIP: 50049 City: city1 URL: url1

    — Ken

Re: list of lists?
by BillKSmith (Monsignor) on Jul 29, 2021 at 10:59 UTC
    I believe that this is what you are trying to do. Note the square brackets (Making References)to create array references and the syntax @$tuple (Using References) to dereference them.
    use strict; use warnings; my @sites=( ["50049", "city1", "url1"], ["52556", "city2", "url2"], ["50219", "city3", "url3"] ); foreach my $tuple (@sites) { my ( $zip, $name, $url ) = @$tuple; ...; }

    Update: Added Links, corrected typo

    Bill
      Thank you very much (everyone), this is just what I wanted - I couldn't figure out the "@$tuple" syntax.
Re: list of lists?
by AnomalousMonk (Archbishop) on Jul 29, 2021 at 11:31 UTC

    Another organizational structure: an array of hashes.

    Win8 Strawberry 5.8.9.5 (32) Thu 07/29/2021 7:25:26 C:\@Work\Perl\monks >perl use strict; use warnings; my @sites = ( { 'zip' => '50049', 'city' => 'city1', 'url' => 'url1', }, { 'zip' => '52556', 'city' => 'city2', 'url' => 'url2', }, { 'zip' => '50219', 'city' => 'city3', 'url' => 'url3', }, ); for my $hr_site (@sites) { my ($url, $zip, $city) = @{ $hr_site }{ qw(url zip city) }; print "doing something in $city $zip with $url \n"; print "also available directly: $hr_site->{'url'} \n\n"; } ^Z doing something in city1 50049 with url1 also available directly: url1 doing something in city2 52556 with url2 also available directly: url2 doing something in city3 50219 with url3 also available directly: url3


    Give a man a fish:  <%-{-{-{-<

Re: list of lists?
by parv (Parson) on Jul 29, 2021 at 05:09 UTC

    Perl (5) does not have a (built in) "tuple" data structure, as found in, e.g, Python. As already shown, use an array of array references instead.

Re: list of lists?
by karlgoethebier (Abbot) on Jul 29, 2021 at 12:33 UTC
    "...I want more information..."

    What about a tiny database like SQLite?

    See also DBI and DBD::SQLite. Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-23 20:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found