Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Replacing a list with one variable

by Anonymous Monk
on Nov 21, 2013 at 10:05 UTC ( [id://1063704]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,
Can you help a Perl novice please. I've searched the internet but can't find an answer or more likely don't know how to ask the right question.
I'm using a JSON request to get information from a remote server. The JSON request requires that I pass a method and a parameter to it. This is working as expected.
My problem is with formatting the parameter so that I can pass a list of varying size to it. This code works:

my $bookID1 = 'abc' my $bookID2 = 'ghj' my $bookID3 = 'zxc' my $param = = { IDs => [ $bookID1,$bookID2,$bookID3 ], sort => "AUTHOR", maxResults => 100 };

There can be a varying amount of bookIDs from 1 to 100, so I wish to build a list that I can then pass to $param.
For example,

my $bookIDList = "abc,ghj,zxc,bnm,qwe,rty,iop,sdf,ert"; my $param = = { IDs => [ $bookIDList ], sort => "AUTHOR", maxResults => 100 };

This code above doesn't work. Any help or suggestions will be appreciated. Thanks
Tony

Replies are listed 'Best First'.
Re: Replacing a list with one variable
by hdb (Monsignor) on Nov 21, 2013 at 10:14 UTC

    It seems that the IDs parameter requires a reference to an array so you need to split your string $bookIDList into an array, the brackets will then turn that into a reference:

    my $bookIDList = "abc,ghj,zxc,bnm,qwe,rty,iop,sdf,ert"; my $param = { IDs => [ split /,/, $bookIDList ], sort => "AUTHOR", maxResults => 100 };

    or you build a list in the first place and feed a reference to that list:

    my @bookIDList = ( "abc", "ghj", "zxc", "bnm", "qwe", "rty", "iop", "s +df", "ert" ); my $param = { IDs => \@bookIDList, sort => "AUTHOR", maxResults => 100 };

      Brilliant, that worked.
      Thank you

Re: Replacing a list with one variable
by Ea (Chaplain) on Nov 21, 2013 at 12:15 UTC
    You might also like the qw() operator which splits a string on whitespace to produce a list. So
    my @bookIDList = qw(abc ghj zxc bnm qwe rty iop sdf ert); my $param = = { IDs => [ @bookIDList ],
    or another way to make a reference to a list
    my $param = = { IDs => \@bookIDList,
    or maybe cut out the assignment line
    my $param = = { IDs => [ qw(abc ghj zxc bnm qwe rty iop sdf ert) ], sort => "AUTHOR", maxResults => 100 };
    I find using the qw() to be quicker and cleaner for me.

    As always, there's more than one way to do it and the best way is the one that makes the code easier to write and read. Express your style :) Modern Perl will give you more ideas.

    The other 3 ways to write this are left as an exercise to the reader

    Sometimes I can think of 6 impossible LDAP attributes before breakfast.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-03-19 08:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found