Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Just another Perl shrine
 
PerlMonks  

goibhniu's scratchpad

by goibhniu (Hermit)
on Aug 13, 2007 at 22:52 UTC ( #632336=scratchpad: print w/ replies, xml ) Need Help??

Random element from concatenation of two arrays:
C:\chas_sandbox\pwgen> copy con randfromtwo.pl my @array1=('1', '2', '3'); my @array2=('a', 'b', 'c'); print $array1[rand(@array1)]; print $array2[rand(@array2)]; print $oneElementOfTheseTwoArrays = (@array1, @array2)[int(rand($#arra +y1+$#array 2))]; ^Z 1 file(s) copied. C:\chas_sandbox\pwgen> randfromtwo.pl 2ba C:\chas_sandbox\pwgen> randfromtwo.pl 1ba C:\chas_sandbox\pwgen> randfromtwo.pl 1c3 C:\chas_sandbox\pwgen> randfromtwo.pl 3b1 C:\chas_sandbox\pwgen> randfromtwo.pl 3c2 C:\chas_sandbox\pwgen>

C:\chas_sandbox> perl -Mdiagnostics -le "sub doit{$ref=$_;${$ref} = 'nope'};$str='cwh'; +print $str ;doit(\$str);print$str" cwh cwh C:\chas_sandbox> perl -Mdiagnostics -le "sub doit{$ref=$_;${$ref} = 'nope'};$str='cwh'; +print $str ;doit(\\$str);print$str" cwh cwh C:\chas_sandbox> perl -Mdiagnostics -le "sub doit{$ref=$_;$$ref = 'nope'};$str='cwh';pr +int $str;d oit(\\$str);print$str" cwh cwh C:\chas_sandbox> perl -Mdiagnostics -le "sub doit{$ref=$_;$$ref = 'nope'};$str='cwh';pr +int $str;d oit(\$str);print$str" cwh cwh C:\chas_sandbox>

Old stuff below here
C:\chas_sandbox> perl -Mdiagnostics -le "sub doit{$ref=$_;\\$ref = 'nope'};$str='cwh';p +rint $str;doit($str);print$str" Can't modify single ref constructor in scalar assignment at -e line 1, + near "'nope'}" Execution of -e aborted due to compilation errors (#1) (F) You aren't allowed to assign to the item indicated, or otherwi +se try to change it, such as with an auto-increment. Uncaught exception from user code: Can't modify single ref constructor in scalar assignment at -e + line 1, near "'nope'}" Execution of -e aborted due to compilation errors. at -e line 1 C:\chas_sandbox>
For ikeagami
This is grammarharness.pl:
#/usr/bin/perl -Wl use strict; use warnings; use Parse::RecDescent; use Data::Dumper; use Data::Serializer; my $grammar; my $grammarfile = $ARGV[0]; open my $gf,'<',$grammarfile or die 'bad grammarfile'; while (<$gf>) { $grammar.=$_; } close $gf; print '$grammar is ',"\n",$grammar; $::RD_HINT++; #$::RD_TRACE++; my $parser = new Parse::RecDescent ($grammar) or die 'bad grammar'; my $stringtoparsefile = $ARGV[1]; open my $sf,'<',$stringtoparsefile or die 'bad stringstoparse file'; my $result; my $reresult; my $serializer = Data::Serializer->new(portable => '0',serializer => + 'Data::Dumper'); my $outfilebasename = 'data'; my $outfileext = 'out'; my $outfileindex = 10; while (<$sf>) { print 'parsing $_: ',$_; if (defined $parser->startrule($_)){ $result = $parser->startrule($_); print Dumper($result); $serializer->store( $result, $outfilebasename.$outfileindex.'.'.$outfileext, '>>' ); $reresult = $serializer->retrieve( $outfilebasename.$outfileindex++.'.'.$outfileext, ); print Dumper($reresult); }else{ print " badstring\n"} }

This is packagegrammar.txt:
startrule: commandline{$item{commandline}} commandline: command options {[$item {command},$item{options}]} command: packagecommand {$item{packagecommand}} packagecommand: /(hap)/ | /(hccmrg)/ | /(hup)/ | /(hspp)/ | /(hpp)/ | +/(hpg)/ | /(hdp)/ | /(hdlp)/ | /(hcp)/ options: option(s?) option: optionflag optionvalue { [$item{optionflag}, $item{optionvalue +}?$item{optionvalue}:1] } optionflag: /(-\w+)/ { $1 } optionvalue: /(\w*)/ { $1 }

This is packagecommands.txt:
hap -b hap -b sknxharvest01 hap -b sknxharvest01 -enc testfile.dfo hap -b sknxharvest01 -usr cgowing -pass chaspass hap -badflag hap -prompt hap -b sknxharvest01 -prompt hap -prompt -b sknxharvest01

All that's working, but I'm thinking of de-serializing and turning the data back into a packagecommand string. I'm thinking Since I wrote the grammar to go one direction, it ought to work the otherway, but haven't seen that it does. P::RD::Deparse is totally irrelevant since it deparses the parser back into a grammar.
What I want is to take $result and turn it back into a command string (hopefully using the same grammar).
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2014-04-20 06:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls