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

Re: Trying to streamline repetitive code....

by kyle (Abbot)
on Oct 29, 2009 at 03:26 UTC ( #803867=note: print w/replies, xml ) Need Help??


in reply to Trying to streamline repetitive code....

That's quite a Wall of Text. I'm guessing this doesn't run under strict.

Anyway, I wrote this just for fun:

use strict; use warnings; use 5.010; my $m8 = m8_text(); eval "no strict;sub { $m8 }"; die $@ if $@; for ($m8) { s{(\@|\$\#)(?!M8PinContentSorted)(\w*M8\w+)}{${1}{\$M->{$2}}}g; s{\$(?!M8PinContentSorted)(\w*M8\w+)\[}{\${\$M->{$1}}\[}g; s{(?<!foreach )\$(?!M8PinContentSorted)(\w*M8\w+)}{\$M->{$1}}g; } eval "no strict;sub { $m8 }"; die $@ if $@; say $m8;

The m8_text() sub returns the text from your node verbatim. I do a few replacements on it, and I confirm before and after that it compiles with eval.

So you can paste the results into a sub like this:

sub node_803848 { my ( $M, $name ) = @_; # wall of text }

Call the sub like node_803848( $big_m{$name}, $name ) with $name set to "M8", for example.

At that point, you need %big_m to have a hash ref for each of your M1–M8, and each of those hashes has an entry for each *M8* variable in the original. There appear to be no hashes in there, so it's all scalars and arrays that you could initialize like so:

my %big_m; $big_m{M8}{M8TrackSummary} = []; # array ref $big_m{M8}{M8CurrentTrackLoc} = undef;

Of course, I haven't tried to run this, but if it works, the result could run under strict with a few more changes. Each use of foreach needs a my. There are places where "M8" appears in string literals, and that needs to be $name.

If I had this in front of me, I'd be strongly tempted to rewrite it all, but I don't have time for that here and now. Instead, I wrote the cheap and evil solution you see above. I don't actually recommend it, but it might get you started.

Good luck.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://803867]
help
Chatterbox?
[ovedpo15]: consider the following format of strings: some_data-doesn't- matter,value. how I get the value with regex? it should be after the last comma (last string)
[Corion]: Text::CSV_XS for all your CSV file parsing needs. Other than that, what problems do you have with perlre, and capturing everything after the comma? Also, index and substr would also work.
[choroba]: my ($value) = $string =~ /.*,(.*)/
[choroba]: The .* at the beginning matches the longest possible substring, so it eats any previous commas
[choroba]: But for CSV, use appropriate modules

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2018-05-27 08:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?