This is PerlMonks "Mobile"

Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

If you have a question on how to do something in Perl, or you need a Perl solution to an actual real-life problem, or you're unsure why something you've tried just isn't working... then this section is the place to ask.

However, you might consider asking in the chatterbox first (if you're a registered user). The response time tends to be quicker, and if it turns out that the problem/solutions are too much for the cb to handle, the kind monks will be sure to direct you here.

Post a new question!

User Questions
Is there a better way to use the line number in a quoted string than __LINE__?
3 direct replies — Read more / Contribute
by davidfilmer
on Mar 07, 2021 at 03:24
    Greetings, Masters. I am often annoyed that this code:

    #!/usr/bin/perl print "Line [ ", __LINE__, " ] looks very nice\n"; print "Line [ __LINE__ ] is taken as a string-literal\n"; #print "Line [ @{[__LINE__}] is a syntax error\n";

    produces this result:

    Line [ 2 ] looks very nice. Line [ __LINE__ ] is taken as a string-literal.

    The syntax of Line 2 works OK, but it is a lot of fanning-about that often makes me reluctant to include this useful bit of debugging information.

    I would much prefer the syntax of Line 3, or some convenient token such as $LINE (or $__LINE__ or whatever I can just embed in double-quotes).

    Line 4 is still a lot of fanning-about, but it doesn't even compile, even though this screwball syntax:

         print "The Time Is: [ @{[scalar(localtime)]} ] right now\n\n";

    works just fine, without dropping out of the double-quotes:

         The Time Is: [ Sun Mar  7 00:14:15 2021 ] right now

    Is there a better way to include the __LINE__ information without all this fanning-about?

    Thanks for reading. I appreciate any help to beautify my ugly code.

    - David
Win32::PowerPoint Extension for Lines and Shapes
1 direct reply — Read more / Contribute
by nzsvz9
on Mar 07, 2021 at 00:53
    I've added a function to the Win32::PowerPoint module successfully to draw a line and astonishingly enough it works:
    sub add_line { my ($self, $options) = @_; return unless $self->slide; $options = {} unless ref $options eq 'HASH'; my $new_line = $self->slide->Shapes->AddLine($options->{x1}, $option +s->{y1}, $options->{x2}, $options->{y2}); return $new_line; }
    I've tried to add a function to draw a shape with very similar extension like this:
    sub add_shape { my ($self, $shape, $options) = @_; return unless $self->slide; $options = {} unless ref $options eq 'HASH'; my $new_shape = $self->slide->Shapes->AddShape($shape, $options->{le +ft}, $options->{top}, $options->{width}, $options->{height}); return $new_shape; }
    But it doesn't work.

    In the horrible MSDN documents on VBA there's discussion about encoded shape names like 'msoShapeRectangle' which is shape enumeration 1, and when I call this function it draws nothing. I've tried calling this new function both the following ways with no success:

    # Add the Rectangle $pp->add_shape('msoShapeRectangle', { left=>100, top=>100, width=>100, + height=>100}); # Add the Rectangle $pp->add_shape(1, { left=>100, top=>100, width=>100, height=>100});
    Neither works.

    Any help or guidance would be apprecaited. There's almost no help in the Win32::PowerPoint in CPAN.

    Also, I've searched for hours for examples on even how to change the color of the line that I can successfully draw - to no avail.

    Kind regards, nzsvz9

    I'm not a coder, I just play one in my job

How to repeat a line thrice by adding a digit into it
4 direct replies — Read more / Contribute
by suvendra123
on Mar 06, 2021 at 21:43
    .march_endSP(march_end), .serial_in_ctlr(serial_in_ctlr) , .memsafe(memsafe) , .mem_atpg_mode(mem_atpg_mode) , .tst_gatedclock(tst_gatedclock) , is there in a text file I want to search a string .march_endSP in a file and repeat that line with .march_endSP_1(march_end) .march_endSP_2(march_end) .march_endSP_3(march_end) in the same file, how to do this This is my code while (my $line = <FH>) { while ( $line =~ /-number_of_SP_mem_supp\s(\d+)/g ) { my $first = $1; #print "$first\n"; my @a = (1..$first); foreach(@a){ print("$_","\n"); } } But I am unable to do using this. Please help me.
How to parse not closed HTML tags that don't have any attributes?
7 direct replies — Read more / Contribute
by Rantanplan
on Mar 06, 2021 at 14:23
    Hi, I'd like to grab phone number and fax number strings from this HTML section:
    <div class="phone"> <div class="icon"></div> <p class="title">Telephone</p> <p>0123-4 56 78 90 <p class="title">Telefax</p> <p> </div>

    Unfortunately, there may, or may not be entries in the fields for the phone and the fax numbers.

    I have tried HTML::TreeBuilder with find_by_attribute and look_down, but can't figure out how to do it.

    Could someone help me please? Many thanks!

Sorting perl hash
3 direct replies — Read more / Contribute
by paul05
on Mar 06, 2021 at 12:37
    I have the below hash structure, hash of hash. I need to sort the inner hash referenced by test1, test2, test3, test4. The outer keys need not be changed.
    $VAR1 = { '191' => { 'test1' => { 'score' => '9.18' }, 'test2' => { 's +core' => '2.84' }, 'test3' => { 'score' => '15.62' }, 'test4' => { ' +score' => '11.84' }, '190' => { 'test1'=> { 'score' => '13.28' }, 'test2' => { 's +core' => '-47.56' }, 'test3' => { 'score' => '18.50' }, 'test4' => { ' +score' => '14.88' } } }
    I am trying to sort the hash based on the 'score' value. The sorting should happen only among whats inside of the main keys '191' and '190' . See below hash structure for expected result.
    $VAR1 = { '191' => {'test3' => { 'score' => '15.62' }, 'test4' => { 's +core' => '11.84' } 'test1' => { 'score' => '9.18' }, 'test2' => { 'sc +ore' => '2.84' }, &#8203; '190' => { 'test3' => { 'score' => '18.50' }, 'test4' +=> { 'score' => '14.88' } 'test1'=> {'score' => '13.28' }, 'test2' => { 'sco +re' => '-47.56' } } }
    Sorting is to be done based on descending score value. I have tried out the below code but it kind-of sorts based on the main-key. I need output as shown in the expected hash structure.
    my @sort_by_rank; for my $key1 (keys %rankBased) { for my $key2 (keys %{$rankBased{$key1}}) { @sort_by_rank = sort{ $rankBased{$b}{$key2}{score} <=> $rankBa +sed{$a}{$key2}{score} } keys %rankBased; } }
    Here %rankBased is the hash. Please help.
Matching a string in a parenthesized block (regex help)
6 direct replies — Read more / Contribute
by maxamillionk
on Mar 05, 2021 at 17:27


    I want to work with a file formatted like this:

    ASDF { tmp plz_match tmp } string2 { tmp } string3 { tmp plz_match tmp }

    I want to know if some arbitrary string (plz_match) exists within one of those parenthesized contexts. I am interested in the block called ASDF and do not want to scan the other blocks.

    I'm not sure why this regex keeps telling me there is no match. I have tried all manner of variations and I am obviously missing something...

    #!/usr/bin/perl use warnings; use strict; my $file = "/path/to/file.txt"; local $/; # added after post open FILE, '<', $file; my $content = <FILE>; close FILE; if ( $content =~ m/(?<=ASDF {)(.*)plz_match(.*?)(?=})/s ) { print "Matched: |$`<$&>$'|\n"; } else { print "No match: |$content|\n";
Introspecting function signatures
3 direct replies — Read more / Contribute
by szabgab
on Mar 05, 2021 at 09:45
    Given a function using signatures like this:
    use strict; use warnings; use 5.020; use feature 'signatures'; no warnings 'experimental::signatures'; sub hello($name, $age) { print "$name\n"; }
    I am looking for a way to list the variables it is expecting during run time. Using the solution from this post using
    I managed to write some code that works for me that I can turn into a CPAN module, but I wonder if there is already one that does that and I missed it?
Ordering Colours Problem
4 direct replies — Read more / Contribute
by merrymonk
on Mar 05, 2021 at 06:08
    This is a list of hexadecimal values of some colours.
    I do know the individual values of the red, green and blue values for each of them.
    Does any Monk know how to order these so that they are in the same order as the colours found on a normal ‘colour wheel?
Which module for finding Equinoxes and Solstices
2 direct replies — Read more / Contribute
by mldvx4
on Mar 05, 2021 at 05:52

    I've been searching a bit and not finding the right module.

    Which module should I be using to calculate summer and winter solstices, and autumn and spring equinoxes?

    That is my main task at the moment, but if there is also a way to calculate Chinese New Year from the same or another module, that will help in a later task.

How can I combine these two regular expressions?
5 direct replies — Read more / Contribute
by thirtySeven
on Mar 04, 2021 at 21:41
    I have a bunch of file paths that look like $path. I want to get the yyyy_mm_dd_hh_mm out of the end of the path. I currently have a working solution ... but I want to know if it is possible to combine the entire thing into one expression if only for learning purposes. I am wondering if it can't work because of the way I am mixing in a list context.
    # This works my $path = '/.snapshots123/yabsm/root/hourly/day=2021_03_04,time=21:20 +'; my $end_of_path = $1 if $path =~ m/([^\/]+$)/; my @nums = $end_of_path =~ m/([0-9]{2,4})/g;
    I have tried to quite literally combine them but it did not work:
    # This does not work my @nums = m/([0-9]+)/g =~ $1 if $path =~ m/([^\/]+$)/;
    I would appreciate insight.

Add your question
Your question:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.