Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Reusing camel code

by gmax (Abbot)
on Dec 02, 2001 at 19:43 UTC ( #128986=perlquestion: print w/ replies, xml ) Need Help??
gmax has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,
This one was a slow Saturday, and I spent some idle time looking at the Camel Code. It would be nice to write such a program with a different shape, I thought. Unfortunately, among my many abilities, drawing is definitely missing. However, since the Camel code is a self drawing program, I thought that its magic, albeit not easily duplicated, could be reused. Thus, I got hold of some shapes (using a separated parser, which skips the first two lines and the __DATA__ section) and managed to re-use the Camel code, according to the golden principle of Laziness, so well explained by Larry Wall.
In order to run this program, you must have the Camel code in your current directory. (I only tried it in Linux and FreeBSD.)
perl camelfilter.pl # shows the llama perl camelfilter.pl shark perl camelfilter.pl gmax
The patterns for shark and llama are taken from the omonimous files in the obfuscated code section of www.perlmonks.net. A third pattern, a rather unimaginative "gmax", I got from rotating the output of the good old Unix banner program (still I can't draw, as I told you before). Parsing these files, I got my patterns as sets of F (for Filled) or S (for Spaces) followed by the number of occurrences. Such patterns are then assigned to scalar variables in camelfilter.pl. Newlines are indicated by colons. Thus a pattern of "S30F12:" means 30 spaces, followed by 12 nonspaces and a newline.
+------------------+ | | | while pattern |<------------------+ <-----------------+ | | | | +------------------+ | | | | | V | | /\ | | / \ +----------+----------+ | / \ | | | / is it\____NO_______> | write next non space| | \ space/ | characters from | | \ ? / | camel.pl | | \ / +---------------------+ | \/ | | | | | YES | | | V | +---------------------+ | | write as many spaces| | | as stated in the +------------------------------------+ | pattern | +---------------------+
camelfilter.pl reads camel.pl and makes a string of its code, skipping all spaces. The code is then rewritten in newcamel.pl following the pattern of llama (default), shark or gmax (if stated as an argument in the command line).
newcamel.pl will inherit camel.pl's magic and print its new shapes, with same additional noise lines (due to the difference in size between camels, llamas, sharks and gmax's, I suppose). You can either get rid of the noise by filtering the unwanted lines
system "perl $newcamel | perl -ne 'print if !/^ ?mm|[\\^]{20}/'";
or show them
do "$newcamel";
There is room for improvement (patterns could be bit-encoded, for instance), but I would like some specific hints on how to apply the pattern in a more linear way than my while ($$pattern ...). I suspect that it might be a mappish solution somewhere, but my Laziness did not come up with any practical suggestion (too much Impatience?, not enough Hybris? I don't know.)
I am working on patterns for a less entertaining project, involving database results representation, and some problems are very similar to this one: applying a pattern to a long stream of data in order to create a complex report. Since the problem is basically the same, I thought that asking with a funny example would at least make someone smile.
TIAFYH
gmax
#!/usr/bin/perl -w # camelfilter.pl use strict; my $llama = "S51F11:S47F10S1F9:S45F21:S49F12:S49F9:S49F9:S49F9:" . "S49F9:S49F9:S47F11:S3F8S4F26S4F13:S1F57:F57:F57:S2F54:S2F54:" . "S3F51:S4F49:S4F48:S5F45:S6F17S3F23:S7F15S6F20:S8F13S10F16:" . "S10F10S14F12:S11F8S17F10:S12F5S20F4S1F4:S12F4S21F4S2F3:" . "S11F4S22F3S3F3:S12F3S22F3S3F3:S13F3S21F3S4F2:S13F5S20F3S3F3:" . "S13F6S19F4S2F5:"; my $shark ="S46F9:S41F18:S8F24S5F3S1F21:F41S2F12:F7S2F33S3F9:" . "S2F22S2F20S2F1S2F4:S4F20S3F22S1F4:S6F2S7F10S1F4S1F3S1F20:" . "S11F2S7F14S1F1S1F15S1F3:S13F3S8F11S1F1S1F1S1F18:" . "S14F7S6F6S1F1S2F21:S16F21S1F9S1F13:S19F20S1F3S2F2S1F13:" . "S19F2S4F14S3F6S2F12:S17F7S9F6S1F2S3F4S1F13:S16F5S17F8S2F1S2F13:" . "S16F1S26F21:S53F12:S48F3S5F9:S50F6S2F7:S52F12:S53F10:S54F6:" . "S54F5:S53F4:S53F2:F1:"; my $gmax = "S65:S12F2S51:S11F4S50:S12F2S51:" . "S5F4S3F2S1F5S3F2S4F2S8F3S7F7S3F5S1:" . "S4F6S1F2S2F5S2F3S3F4S5F7S5F7S3F5S1:" . "S3F3S2F3S5F4S1F5S1F5S5F2S2F3S6F5S6F2S2:" . "S2F3S3F4S4F10S1F6S3F3S3F3S6F4S5F2S3:" . "S1F4S4F3S4F5S1F5S2F4S3F3S3F4S5F4S5F1S4:" . "S1F4S4F4S3F4S3F4S2F4S3F3S4F3S6F4S3F2S4:" . "S1F3S6F3S3F4S3F3S3F4S3F3S4F3S6F4S3F2S4:" . "F4S6F3S3F4S3F3S3F4S4F1S5F3S7F4S2F1S5:" . "F4S6F3S3F4S3F3S3F4S10F3S7F4S1F2S5:" . "F4S6F3S3F4S3F3S3F4S10F3S8F5S6:" . "F4S6F3S3F4S3F3S3F4S5F2S3F3S8F5S6:" . "F4S6F3S3F4S3F3S3F4S4F5S1F3S8F5S6:" . "S1F4S4F4S3F4S3F3S3F4S3F10S8F5S6:" . "S1F4S4F4S3F4S3F3S3F4S2F4S2F5S9F4S6:" . "S2F3S4F3S4F4S3F3S3F4S2F3S4F4S9F4S6:" . "S2F3S3F3S5F4S3F3S3F4S2F3S5F3S8F6S5:" . "S3F7S6F4S3F3S3F4S1F4S5F3S8F6S5:" . "S4F6S6F4S3F3S3F4S1F4S5F3S8F1S2F4S4:" . "S4F1S11F4S3F3S3F4S1F4S5F3S7F2S2F4S4:" . "S3F2S11F4S3F3S3F4S2F3S5F3S7F1S4F4S3:" . "S2F3S11F4S3F3S3F4S2F3S4F4S6F2S4F4S3:" . "S2F4S10F4S3F3S3F4S2F4S3F4S6F2S4F4S3:" . "S2F6S8F4S3F3S3F4S3F11S4F2S6F4S2:" . "S2F7S6F6S1F5S1F6S2F6S1F4S2F5S4F6S1:" . "S2F9S4F6S1F5S1F6S4F2S4F4S1F5S4F6S1:" . "S2F10S53:S1F2S2F7S53:S1F1S4F7S52:" . "S1F1S6F5S52:F2S8F3S52:F2S8F3S52:" . "F2S8F3S52:F2S8F3S52:S1F1S8F3S52:" . "S1F2S6F3S53:S2F3S3F4S53:S2F9S54:S3F7S55:S65:"; my $newcamel = "newcamel.pl"; my $oldcamel = "camel.pl"; open CAMEL, "< $oldcamel" or die "camel ($oldcamel) not found\n"; open NEWCAMEL, "> $newcamel" or die "can't create new camel ($newcamel)\n"; my $camel; while (<CAMEL>) { print NEWCAMEL $_,next if /^(:?use|#)/; chomp; while (/(.)/g) { $camel .= $1 if $1 ne " "; } } close CAMEL; my $out; my $choice=shift; my $pattern = \$llama; if (defined $choice) { $pattern = \$gmax if $choice eq "gmax"; $pattern = \$shark if $choice eq "shark"; } else { $choice = "llama"; } my $camelcount =0; while ($$pattern =~ /([SF:])([^SF:]*)/g) { if ($1 eq "F"){ $out = substr ($camel, $camelcount, $2); $camelcount += $2; } elsif ($1 eq "S"){ $out = " " x $2; } else { $out = "\n"; } print NEWCAMEL $out; } $out = substr($camel,$camelcount); print NEWCAMEL $out; close NEWCAMEL; system "perl $newcamel | " . "perl -ne 's/camel/$choice/;print if !/^ ?mm|[\\^]{20}/'"; #do "$newcamel";
Edit 2001-12-05 by dvergin per user request</link>

Comment on Reusing camel code
Select or Download Code
Re: Reusing camel code
by chromatic (Archbishop) on Dec 03, 2001 at 00:00 UTC
    split looks like a better choice than a regex. I whittled away at this and came up with the untested:
    for (split(/:/, $$pattern)) { # make sure it starts and ends with spaces $_ = 'S0' . $_ unless /^S/; $_ .= 'S0' unless /S\d+$/; my @pieces = split(/[SF](\d+)/, $_); while (my ($spaces, $fills) = splice(@pieces, 0, 2)) { $out .= " " x $spaces . substr($camel, $camelcount, $fills); } $out .= "\n"; }
    I'm fairly pleased with that, but might try to fit a pack in there. (How would you have used map?)

    Aside, this:

    while (/(.)/g) { $camel .= $1 if $1 ne " "; }
    could be spelled:
    tr/ //d; $camel .= $_;
    I'm sure you can trim that down more, too. Don't forget to check out Acme::EyeDrops for a different take on things.

    Update, very shortly after: You could, of course, just split on [SF] and probably get the same effect, though you'll have an empty element at the start. It may or may not be more clear that way.

      Thank you very much for your hints. I tested your code
      for (split(/:/, $$pattern)) { $_ = 'S0' . $_ unless /^S/; $_ .= 'S0' unless /S\d+$/; print STDERR "pattern: $_\n"; my @pieces = split(/[SF](\d+)/, $_); foreach my $count (0..$#pieces) { print STDERR "$count -> [$pieces[$count]] "; } print STDERR "\n"; }
      But I got an odd output. Split gives back some empty items
      pattern: S51F11S0 0 -> [] 1 -> [51] 2 -> [] 3 -> [11] 4 -> [] 5 -> [0]
      Changing split to match only /[SF]/ (your update suggestion) and adding a shift afterwards improves the output,
      pattern: S51F11S0 0 -> [51] 1 -> [11] 2 -> [0]
      leaving me with the second problem, i.e. that having an odd number of items in my array, splice(@,0,2) will give back an undefined second element.
      Nothing catastrophic, but it is going to increase the number of necessary checks.
      Even if it doesn't do what I need, though, this code of yours gives me some ideas that I can further develop.
      As for the second hint:
      tr/ //d; $camel .= $_;
      yes, it looks much faster (not to mention smarter) than my code.
      Ciao gmax
Re: Reusing camel code
by Beatnik (Parson) on Dec 03, 2001 at 00:02 UTC
    #!/usr/bin/perl eval eval '"'. ('#'). '!'.'/' .('[' ^'.' ) .('['^'('). ("\["^ ')')."\/".( '`'|'"').('`'| ')').( '`'|'.') .'/'. ( '['^'+').('`'|'%').('[' ^')').('`'|',').('!' ^'+').('!'^'+').('['^'+' ).('['^')').('`'|')') .('`'|'.').('['^'/').(('{')^ '[').'\\'.'"'.("\`"^ '(').('`'|'%').('`'|',').('`'|','). (('`')| '/').('{'^'[').('{'^',').('`'|'/').( '['^')').('`'|',').('`'|'$').'\\'.'"'.';'.'"';$:='.'^'~';$~ ='@'|'(';$^=')'^'[';$/='`'|'.';$_='('^'}';$,='`'|'!';$\="\)"^ '}';$:='.'^'~';$~='@'|'(';$^=')'^'[';$/='`'|'.';$_='('^"\}";$,= '`'|'!';$\=')'^'}';$:='.'^'~';$~='@'|'(';$^=')'^'[';$/='`'|'.'; $_='('^'}';$,='`'|'!';$\=')'^'}';$:='.'^'~';$~='@'|'(';$^="\)"^ '[';$/='`'|'.';$_='('^'}';$,='`'|'!';$\=')'^'}';$:='.'^"\~";$~= '@'|'(';$^=')'^'[';$/='`'|'.';$_='('^'}';$,='`'|'!';$\=')'^"\}"; $:='.'^'~';$~='@'|'(';$^=')'^'[';$/='`'|'.';$_='('^'}';$,="\`"| '!';$\=')'^'}';$:='.'^'~';$~='@'|'(';$^=')'^'[';$/='`' |"\."; $_='('^'}';$,='`'|'!';$\=')'^'}';$:='.'^'~';$~="\@"| "\("; $^=')'^'[';$/='`'|'.';$_='('^'}';$,='`'|'!';$\=')' ^'}'; $:='.'^'~';$~='@'|'(';$^=')'^'[';$/='`' |'.';$_= '('^ (( '}'));$,='`'|'!';$\=')'^"\}";$:= '.'^'~'; ($~) ='@'|"\("; $^=')'^'[' ;$/='`'| '.';$_= '(' ^('}');$,= '`'|'!';$\ =')'^'}' ;$:='.' ^+ "\~";$~= '@'|'(';$^ =(')')^ '[';$/ ='`'|'.' ;$_=('(')^ '}';$,= "\`"| '!';$\= ')'^'}';$: ="\."^ '~';$~ =('@')| "\(";$^= (')')^ "\["; $/='`' |"\."; $_='(' ^'}'; ($,)= ('`')| '!';$\ =')'^ "\}"; $:='.'^ '~';$~ ='@' |'('; $^=')'^'[' ;$/= '`'| "\.";$_= '('^ '}'; $,=('`')| '!'; ($\) =')'^'}';$: ='.'^ "\~"; $~='@'|'(';$^= (')')^ "\["; $/="\`"| '.'; $_='('^ '}';$,= '`'|'!'; $\="\)"^ '}';#;
    Now that I have your attention, the above camel is generated with Acme::EyeDrops and it's one of the many shapes available. You can have your own ASCII art too :) BooK explained the REAL camel at YAPC::Eu 2.00.1. Slides are here.

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (11)
As of 2014-09-19 09:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (133 votes), past polls