Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Several weeks ago I was playing around with glob's ability to create lists from stringified lists. (The forth paragraph in the doc.) I at first wanted to see if I could feed it arrays directly, but I was shown by jeffa the lists had to be comma separated strings. After playing around with it for a few minutes, I thought about finding a way use arrays, so wrote the following.

Note: I do not like the name of the subroutine, so I am open to suggestions.

sub glob_array { my (%opt) = @_; my $joiner = $opt{'joiner'} ? $opt{'joiner'} eq ' ' ? "' '" : $opt{ +'joiner'} : ''; my $space = $opt{'space holder'} ? $opt{'space holder'} : '_'; # The arrays have to be strinified into comma separated lists. Thank + you to jeffa for showing me. my $arrays = [map { $_ = '{'.join( ',', @{$_} ).'}'; $_ =~ s/ /$spac +e/g; $_; } @{$opt{'arrays'}}]; my $string = join( $joiner, @$arrays ); my @raw_array = glob "$string"; my @array = map { $_ =~ s/$space/ /g; $_; } @raw_array; return \@array; }
  • The joiner is the character you wish to use to join the values of the arrays together. (If the joiner is a space, it has to be quoted, hence the complicated line of code for it.)
  • The space holder converts the spaces in the values into the character of your choice. Spaces in the array values will muck up your output. (Sorry I can't explain that better.)
  • The arrays are the list of array (refs) you want to use to make the list.

Here are some sample arrays and how they would look once glob has done its work.

my @colors = qw(red green blue); my @directions = qw(north south east west); my @numbers = (1..3); my $array = glob_array( 'arrays' => [\@colors, \@directions, \@numbers +], 'joiner' => ' ', 'space holder' => '__' );

Output

$VAR1 = [ 'red north 1', 'red north 2', 'red north 3', 'red south 1', 'red south 2', 'red south 3', 'red east 1', 'red east 2', 'red east 3', 'red west 1', 'red west 2', 'red west 3', 'green north 1', 'green north 2', 'green north 3', 'green south 1', 'green south 2', 'green south 3', 'green east 1', 'green east 2', 'green east 3', 'green west 1', 'green west 2', 'green west 3', 'blue north 1', 'blue north 2', 'blue north 3', 'blue south 1', 'blue south 2', 'blue south 3', 'blue east 1', 'blue east 2', 'blue east 3', 'blue west 1', 'blue west 2', 'blue west 3' ];

I have yet to figure out how to make this more interesting by including a pattern or a single joiner. From the above output, instead of plain green west 2 having green sector west tower 2nd floor or something akin to it. I could pre-munge the lists, but adding the ability to use a pattern would be nice too.

So, I would like a new name for the subroutine and maybe some pointers on how to add in a pattern to this. Also, maybe a few good reasons to use this I may not be seeing other than making a list to use for random selection.

No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
Lady Aleena

In reply to RFC: Playing with glob by Lady_Aleena

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-18 04:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found