Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Generate list of possibilities from incomplete input

by Pascal666 (Scribe)
on Jan 21, 2018 at 00:25 UTC ( #1207609=perlquestion: print w/replies, xml ) Need Help??

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

Given a string containing 9 hexadecimal characters, I need to generate a list of all possible 10 character hexadecimal strings that contain the given 9 characters in order. The list need not be in any specific order.

I think the complete list will have 16x10=160 items, but 9 of those will be duplicates. Don't worry about removing the duplicates, but if your solution doesn't include them that is fine too.

Obviously, this can be done with a couple nested loops and string concatenation. Somehow, that just doesn't feel like the best way to me though.

For example, given 0ae4bb830, the list would include:

00ae4bb830 10ae4bb830 20ae4bb830 30ae4bb830 40ae4bb830 50ae4bb830 60ae4bb830 70ae4bb830 80ae4bb830 90ae4bb830 a0ae4bb830 b0ae4bb830 c0ae4bb830 d0ae4bb830 e0ae4bb830 f0ae4bb830 00ae4bb830* 01ae4bb830 02ae4bb830 03ae4bb830 04ae4bb830 ...
*Duplicate

Replies are listed 'Best First'.
Re: Generate list of possibilities from incomplete input
by tybalt89 (Prior) on Jan 21, 2018 at 01:30 UTC

    Well, it's still a couple of loops :)

    #!/usr/bin/perl use strict; use warnings; $_ = '0ae4bb830'; my @list; push @list, map "$`$_$'", 0..9, 'a'..'f' while /()/g; print scalar @list, "\n\n@list\n";
Re: Generate list of possibilities from incomplete input
by johngg (Canon) on Jan 21, 2018 at 01:03 UTC

    You could use glob with substr although it gives a warning.

    johngg@shiraz ~/perl/Monks $ perl -Mstrict -Mwarnings -E ' my $str = q{0ae}; my $globStr = q/{/ . join( q{,}, 0 .. 9, q{a} .. q{f} ) . q/}/; for my $off ( 0 .. length( $str ) + 1 ) { my $new = $str; substr $new, $off, 0, $globStr; say for glob $new; }' 00ae 10ae 20ae 30ae 40ae 50ae 60ae 70ae 80ae 90ae a0ae b0ae c0ae d0ae e0ae f0ae 00ae 01ae 02ae 03ae 04ae 05ae 06ae 07ae 08ae 09ae 0aae 0bae 0cae 0dae 0eae 0fae 0a0e 0a1e 0a2e 0a3e 0a4e 0a5e 0a6e 0a7e 0a8e 0a9e 0aae 0abe 0ace 0ade 0aee 0afe 0ae0 0ae1 0ae2 0ae3 0ae4 0ae5 0ae6 0ae7 0ae8 0ae9 0aea 0aeb 0aec 0aed 0aee 0aef substr outside of string at -e line 7.

    I hope this is useful.

    Update: This gets rid of the warning.

    johngg@shiraz ~/perl/Monks $ perl -Mstrict -Mwarnings -E ' my $str = q{0ae}; my $globStr = q/{/ . join( q{,}, 0 .. 9, q{a} .. q{f} ) . q/}/; for my $off ( 0 .. length $str ) { my $new = $str; substr $new, $off, 0, $globStr; say for glob $new; } $str .= $globStr; say for glob $str;' 00ae 10ae 20ae 30ae 40ae 50ae 60ae 70ae 80ae 90ae a0ae b0ae c0ae d0ae e0ae f0ae 00ae 01ae 02ae 03ae 04ae 05ae 06ae 07ae 08ae 09ae 0aae 0bae 0cae 0dae 0eae 0fae 0a0e 0a1e 0a2e 0a3e 0a4e 0a5e 0a6e 0a7e 0a8e 0a9e 0aae 0abe 0ace 0ade 0aee 0afe 0ae0 0ae1 0ae2 0ae3 0ae4 0ae5 0ae6 0ae7 0ae8 0ae9 0aea 0aeb 0aec 0aed 0aee 0aef 0ae0 0ae1 0ae2 0ae3 0ae4 0ae5 0ae6 0ae7 0ae8 0ae9 0aea 0aeb 0aec 0aed 0aee 0aef

    Update 2: Shouldn't post late at night when brain already half asleep! The warning was because I was being an idiot, length( $str ) + 1 should be length $str of course :-/

    johngg@shiraz ~/perl/Monks $ perl -Mstrict -Mwarnings -E ' my $str = q{0ae}; my $globStr = q/{/ . join( q{,}, 0 .. 9, q{a} .. q{f} ) . q/}/; for my $off ( 0 .. length $str ) { my $new = $str; substr $new, $off, 0, $globStr; say for glob $new; }' 00ae 10ae 20ae 30ae 40ae 50ae 60ae 70ae 80ae 90ae a0ae b0ae c0ae d0ae e0ae f0ae 00ae 01ae 02ae 03ae 04ae 05ae 06ae 07ae 08ae 09ae 0aae 0bae 0cae 0dae 0eae 0fae 0a0e 0a1e 0a2e 0a3e 0a4e 0a5e 0a6e 0a7e 0a8e 0a9e 0aae 0abe 0ace 0ade 0aee 0afe 0ae0 0ae1 0ae2 0ae3 0ae4 0ae5 0ae6 0ae7 0ae8 0ae9 0aea 0aeb 0aec 0aed 0aee 0aef

    Cheers,

    JohnGG

Re: Generate list of possibilities from incomplete input
by BillKSmith (Prior) on Jan 21, 2018 at 04:09 UTC
    Sometimes brute force is best. Here two foreach loops iterate the parameters of substr. The function uniq in the module List::Util removes the duplicates.
    use strict; use warnings; use List::Util qw(uniq); my $given_string = '0ae4bb830'; my @results; foreach my $pos ( 0 .. 9 ) { foreach my $chr ( '0' .. '9', 'a' .. 'f' ) { my $out = $given_string; substr( $out, $pos, 0 ) = $chr; push @results, $out; } } { local $, = "\n"; print uniq(@results); }
    Bill

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2020-10-23 02:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (234 votes). Check out past polls.

    Notices?