Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
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
The error says the value is uninitialized, but it works anyway
4 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?
1 direct reply — 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.
Infinite LOOP and reading from STDIN
5 direct replies — Read more / Contribute
by Glivter
on Aug 13, 2019 at 08:24
    Hi Guys, Im writing a "Simple" text game in perl for learning purpose.

    I have this problem. I want to create a timer that can do some background stuff like adding player hp / mana / anything but im not sure how to create it at this moment and don't know if this is the right part of code to execute subroutine for that. And main problem is that Heartbeat is execute only once.

    This is the code snipped for reading player input.

    while (1) { Engine::doHeatbeat; chomp (my $input = <STDIN>); # Trim right / left white character $input =~ s/^\s+|\s+$//g; given ($input) { Engine::Parser::Command($input); } }
    The Heartbeat subroutine is at this moment dummy.. Its my first time here so sorry if i write the question wrong ;)
About using rperl
3 direct replies — Read more / Contribute
by hakonhagland
on Aug 13, 2019 at 05:13
    I just saw this video https://www.youtube.com/watch?v=NyphRo5roV0 about RPerl. Maybe I am missing something, but how can this be useful?
    • If I want fast code, I can write it directly in C++, why would I use RPerl?
    • As I understand it, I cannot use CPAN modules with rperl? They first have to be rewritten to comply with the restricted sub set of Perl? It feels like this is a major issue
    • If I find a piece of a large perl program that needs optimization, how can I use rperl to optimize only that part of the code? Do I compile the code first into a shared library, and then load it at run time using DynaLoader? Then, why not just write an XS-module?
    Note: I am not trying to criticize the project. I found it very interesting. I am just trying to understand how it can be used in practice. Thanks!
Getting pixel colours with Image::Magick
2 direct replies — Read more / Contribute
by Cody Fendant
on Aug 11, 2019 at 04:28

    I'm using Image::Magick to get the individual colour of each pixel of an image.

    What I'm getting back when I do this:

    GetPixel( 'x' => $x_position, 'y' => $y_position );

    Is something like this:

    [ '0.980392156862745', '0.996078431372549', '0.996078431372549' ]

    And I'm having to turn that into a hexadecimal code myself with a subroutine where each value is translated with

    sprintf( "%02x", $_ * 255 );

    So, is there any way I can get back hex values from GetPixel?

    I'm a little confused about where full documentation for the perl ImageMagick module could be found. The stuff on CPAN is quite brief (doesn't even mention GetPixel), and the stuff on the ImageMagick site doesn't really help.

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 examining the Monastery: (2)
As of 2019-08-18 05:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If you were the first to set foot on the Moon, what would be your epigram?






    Results (134 votes). Check out past polls.

    Notices?