Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

The Monastery Gates

( #131=superdoc: print w/replies, xml ) Need Help??

Donations gladly accepted

If you're new here please read PerlMonks FAQ
and Create a new user.

New Questions
Undef value in Template Toolkit
No replies — Read more | Post response
by DreamT
on Aug 20, 2019 at 10:13
    Hi,
    I want to set a value to undef in a TT template:
    [% hashref.value1 = undef %]
    , but this only produces an empty string.
    $VAR1 = { ... 'value1' => '' ... }

    Is there a way around this? From what I've read it seems to be a "feature" in Template Toolkit that undefs are treated as empty strings.
minimal superstrings/maximal substrings
4 direct replies — Read more / Contribute
by stevena
on Aug 19, 2019 at 20:23

    Given a lexicon with long words and short words (happening to be length 9 and up, and 8 and down, respectively), I am looking to enumerate all minimal extensions of a short by a long; that is, with no intervening word (either long or short) that extends the short and is extended by the long. (Extensions could also be called strict superstrings.)

    I.e., if   'w > x' means word w extends word x, I want to find all pairs (L, S), such that:

    * L > S

    * there's no W with L > W > S,

    where L, S, W are restricted to lexicon words.

    For example, (NUMERICAL, NUMERIC) is desired, but not (SOCIOBIOLOGIST, OLOGIST) because SOCIOBIOLOGIST > BIOLOGIST > OLOGIST.

    I am looking for transparent code conducive to my adding less interesting constraints. (Fine if you guide me without writing the code. Also happy to see code.)

    Not homework or work -- solely hobby-related. Thanks!

Defining an XS symbol in the Makefile.PL
3 direct replies — Read more / Contribute
by syphilis
on Aug 18, 2019 at 00:59
    Hi,
    In an XS file I have the following line of code:
    printf("%.16e", sqrt(2.0));
    I want to rewrite that line as:
    printf(MY_FORMAT, sqrt(2.0));
    I also wish to then define MY_FORMAT to "%.16e" in the Makefile.PL.

    This is normally achieved by assigning an appropriate value to DEFINE in the WRITEMAKEFILE() section of the Makefile.PL - eg:
    WRITEMAKEFILE( .... DEFINE => '-DMY_FORMAT="%.16e"', .... );
    But that specific incantation fails to work - and I haven't been able to hit on the incantation that *does* work.
    Update: By "not work", I mean that it either won't compile or it leads to runtime crashes (depending upon the actual incantation I've tried).
    Any help would be much appreciated.

    I've also spent a couple of hours trying to get this working in Inline::C. If I can get it to work in Inline::C, then I'll be able to use the incantation in the Makefile.PL that Inline::C generates.
    Here's that Inline::C script:
    use strict; use warnings; use Config; use Inline C => Config => USING => 'ParseRegExp', CCFLAGSEX => '-DMY_FORMAT="%.16e"', BUILD_NOISY => 1, ; use Inline C =><<'EOC'; SV * foo(void) { printf(MY_FORMAT, sqrt(2.0)); printf("\n"); } EOC foo();
    I've tried various escapes around "%.16e" but nothing has been successful.
    In C, it's pretty simple to achieve:
    C:\_32\C>type try.c #include <stdio.h> #include <math.h> int main(void) { printf(MY_FORMAT, sqrt(2.0)); return 0; } C:\_32\C>gcc -o try.exe try.c -DMY_FORMAT=\"%.16e\" C:\_32\C>try 1.4142135623730951e+000 C:\_32\C>
    I'm not all that bothered if it turns out that Inline::C can't handle the construct, but I *would* like to know how to do it in the XS file via the Makefile.PL if, indeed, that's possible at all.

    Cheers,
    Rob
The error says the value is uninitialized, but it works anyway
8 direct replies — Read more / Contribute
by mizducky
on Aug 17, 2019 at 05:29
    This is a homework assignment, and it works. If I use strict, I don't get an error, but if I use warnings, it says "Use of uninitialized value in string eq at line 7", it prints this error twice (I assume its for the two times that the match is found).
    Here is the code I am turning in for my assignment, but I still want to know why its giving me those errors, and if I should do something better. So I'm really just hoping for someone to help me understand what is happening to cause this error.
    my @colors = qw(red green blue yellow pink purple brown); my $count = @colors; my @drop = qw(pink brown); my $num = 0; foreach $num (1..$count){ $num--; if ($colors[$num] eq $drop[0] or $colors[$num] eq $drop[1]){ splice (@colors, $num, 1); } } print "@colors \n";
Portable way to determine if two names refer to the same file?
2 direct replies — Read more / Contribute
by jcb
on Aug 15, 2019 at 23:31

    I have spent the past hour or so trying Super Search and not finding a clear answer, so I ask my fellow monks how best to portably determine if two seemingly different names actually refer to the same physical file?

    I am not concerned about copies of the same file, only links, such that the same physical file appears under multiple names.

    On POSIX, the solution is easy: compare dev:ino tuples from the stat builtin and declare "same file" if they match. I have no idea if this also works on Windows or even if the problem exists on Windows — how well does Windows handle symlinks anyway and does it even support hardlinks at all?

    And what of the less-common platforms?

    Cross-posted in Categorized Questions and Answers at How do I portably determine if two filenames refer to the same file? as a place to collect answers for future reference.

    Edited 2019-08-16 by jcb: Clarify that stat refers to the Perl builtin. I had forgotten about the shell command with the same name.

List comparison problem
2 direct replies — Read more / Contribute
by perlmonkster
on Aug 15, 2019 at 22:12

    Hello

    I somehow lost a final version of a simple Perl script to compare 2 lists, and cannot seem to figure out what is causing a problem with the version I have.

    The simple .pl script takes ListL.txt, compares it to ListH.txt, and then "Flags" any entries from ListH.txt that are on ListL.txt (plus gives two separate Counts at the bottom of the output). Using two short sample lists, the Counts are both correct, but for some reason one of the ListL.txt items that *should* show a ListH.txt "Flag" in the output does not. I've tried switching around Count statements, etc., but am completely baffled. Any insight as to how to fix things would be greatly appreciated.

    Here's the short code

    use strict; use warnings; my %H_list; open my $H_list, '<', 'listH.txt' or die "Cannot open listH.txt: $!"; + while (my $line = <$H_list>) { chomp $line; $line =~ s/\r//g; # removes windows CR characters $line =~ s/\s+$//; # removes trailing white spaces $H_list{$line} = 1 } close $H_list; my ($L_count, $H_count); open my $L_list, '<', 'listL.txt' or die "Cannot open listL.txt: $!"; + while (<$L_list>) { chomp; s/\r//; s/\s+$//; $L_count ++; print; $H_count ++ and print ' On List H' if exists $H_list{$_}; print "\n"; } print "List L UNIQUES: $L_count; FLAGGED From List H: $H_count \n";

    Here are the two short Test Lists and Test output:

    (ListL.txt) ABC123 DEF456 GHI789 (ListH.txt) ABC123 GHI789 (Test Output) ABC123 DEF456 GHI789 On List H List L UNIQUES: 3; FLAGGED From List H: 2

    As you can see, ABC123 should be also "Flagged" as "On List H", and is driving me NUTS as to why not.

    Thanks very much.

    -perlmonkster
Regular expression for finding acronyms
2 direct replies — Read more / Contribute
by mldvx4
on Aug 15, 2019 at 22:07

    I'm trying to build a pattern that will extract acronyms. I've gotten fairly close, but it leaves off the final period where it should be saved. Notice that it is missing from the output. For example, "X.H.T.M.L" should be "X.H.T.M.L."

    I wrote the pattern below in a moment of insight and now that moment has passed. How may I modify it to preserve the trailing periods?

    #!/usr/bin/perl use strict; use warnings; while (<DATA>) { chomp; while( s/(?'foo' ( (?=([[:upper:]]\.\s){2})[[:upper:]\.\s]{2,} | (?=([[:upper:]]\s){2})[[:upper:]\s]{2,} | (?=([[:upper:]]\.){2})[[:upper:]\.]{2,} | [[:upper:]] ){2,} )//x ) { my $acronym = $+{foo}; print qq("$acronym"\n); } } exit(0); __DATA__ L F and LF and L.F. and L. F. and not L, F. some HTML some XML. or X.H.T.M.L. or X. H. T. M. L. or even X H T M L but not U and I, or You and I. ...

    Or if there is an existing function or module which does that already, I can use that instead.

Python 'is' command
8 direct replies — Read more / Contribute
by betmatt
on Aug 14, 2019 at 07:37
    Dear Monks,

    I've just discovered a very interesting command in Python. The 'is' command. It is not the same as '==' because 'is' does not check for equality. Instead it checks whether the same memory reference is used in each case. The theoretical basis for using the 'is' command is quite nice in many circumstances (particularly in relation to object orientated programming). I am assuming that you can do exactly the same in Perl using a ref command in conjunction with '=='. It is a classic case of where the theory overrides the non requirement at the code level. It is worth being aware of as a Perl programmer. Personally I think Python has it right on that one, although I do think that Python has too many commands. Are there any other examples like this that I can follow? It might make me a better Perl programmer.

Complex file manipulation challenge
4 direct replies — Read more / Contribute
by jdporter
on Aug 13, 2019 at 12:53

    One of the packages in Scaladex (Scala's equivalent of CPAN, I guess) states its motivation in the form of the following problem:

    1. List all .csv files in a directory by increasing order of file size
    2. Drop the first line of each file and concat the rest into a single output file
    3. Split the above output file into n smaller files without breaking up the lines in the input files
    4. gzip each of the smaller output files

    How would you do this in Perl?

    I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.
New Cool Uses for Perl
Moving, copying and renaming files with new tool
No replies — Read more | Post response
by siberia-man
on Aug 14, 2019 at 05:22
    Hello Monks,

    I re-invented the wheel and decided to share it. This is script that is supposed to be used as a tool for moving, copying and renaming files. In the beginning I called it as "the re-invented wheel" becuase there are a few implementations for such kind of functionality. I found 3 of them at least (all are mentioned in the documentation). While developing the script I borrowed some good ideas from those implementations and adapted for my script. And I applied my vision of the conveniency.

    Here I show some scenarios from real life I've really met:

    Removing prefixes and suffixes:

    file-rename 's/^[^.]+\.//; s/\.[^.]+$//' ...
    Enumerate files:
    file-rename 's/^/sprintf "%02d. ", $NR/' ...

    By default the script implements move files, but it is possible to copy them with the option -c, --copy.

    It is posible to include/exclude Perl modules with the option -M for those cases it you need to apply something very specific. It is similar to Perl's own option.

    With the -T or --transcode option it is possible to apply encoding over names. For example the following example works fine for filenames in Cyrillic with Perl 5.14 under Cygwin 1.7.25:

    file-rename -Tutf8 '$_ = ucfirst' -f ...

    Handling with filename component is enabled with the option -N, --filename-only. The is example (prepending filenames with some prefix):

    file-rename 's/^/old-/' -N ../*

    Verbosity, forcing and dry-run are implemented with the -v, -f and -n options, respectively. The long options are also available

    I have still never met the case of using the zero-terminated lines but implemented it with the options -z, -0, --null.

    The last thing I developed is renaming in loop with the option -r, --rename. With this option we can:

    Rotate files cyclically to left (resulting to file2 file3 file4 file1):

    file-name --rename=rotate-left file1 file2 file3 file4
    Rotate files cyclically to right (resulting to file4 file1 file2 file3):
    file-name --rename=rotate-right file1 file2 file3 file4
    Swap pair of files (swap nearest, resulting to file2 file1 file4 file3):
    file-name --rename=swap file1 file2 file3 file4
    Flip the whole list of files (swap farthest, resulting to file4 file3 file2 file1):
    file-name --rename=flip file1 file2 file3 file4

    The script lives in github. Below is the latest version to the moment of the writing.

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 avoiding work at the Monastery: (5)
As of 2019-08-20 14:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?