Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Seekers of Perl Wisdom

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

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. Post a new question!

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.

User Questions
regex help
1 direct reply — Read more / Contribute
by Anonymous Monk
on Aug 02, 2015 at 01:03
    I have a text below and along with this there is some more text which is p tags, br tags. Please help me to remove entire sentence if contains 23456.
    <p>Find more business news at and Text BUSIN +ESS to 23456 for breaking business news text alerts on your mobile ph +one. Text JOBS to 23456 for job alerts.</p> <p>Want your news even faster? Text NEWS to 23456 to sign up for break +ing news text alerts. See for a complete list of alerts.</p> <br>SIGN UP FOR MOBILE NEWS ALERTS! Get your news on the go, text NEWS + to 23456
Help fixing this piece of code
4 direct replies — Read more / Contribute
by perlynewby
on Aug 01, 2015 at 17:28

    Hello Monks, I am interested in finding out how to get this specific subroutine to work.

    I have worked it out using other method but very interested in finding the method to this specific hash format below since I couldn't find the solution myself

    Since I couldn't fix it myself and still interested to see to see how you guys do it

    sub routine must find the MODE in a given set of numbers. must find the most duplicated number and pass the value.

    data 2 3 3 3 5 7 8 12 32 44 55 12 3 23 43 33 1 4 25 43 42 1 4 5 3 3 3
    sub mode_num{ my %count=(); %opt = %{shift @_ } ; #creating hash #dump \%opt; #check data coming in.. #need to get integrate count as I format data into hash(next task + to make it reable) for (sort{$a <=> $b} %opt{NUMBERS}){ #dump \$opt{NUMBERS}++; $opt{NUMBERS}++; } our @keys= sort{$opt{NUMBERS}->{$b}<=>$opt{NUMBERS}->{$a}} key +s %opt{NUMBERS}; return }
Why am I getting message «Operation "<=>": no method found,» ?
4 direct replies — Read more / Contribute
by RCH
on Aug 01, 2015 at 10:44

    Dear Perl Monks

    I'm getting the message

    Operation "<=>": no method found, left argument in overloaded package Class::Measure::Length, right argument in overloaded package Class::Measure::Length

    from what I thought was just a simple sort


    I found a work-around, by sprintf-ing the values in the hash - see code below - but I would like to (try to) understand what is going on.

    I'm on Ubuntu 12.04

    perl -v says

    This is perl 5, version 14, subversion 2 (v5.14.2) built for i686-linux-gnu-thread-multi-64int
    The following script gives the no method found message the second time round
    use strict; use Carp; use GIS::Distance; my $from_lat = 50.815717; my $from_long = 4.436326; my $to = { Montana => [ 46.536026, -111.935062 ], Correze => [ 45.341874, 1.79531 ], ParisFR => [ 48.850407, 2.347304 ], }; my %distance; foreach my $demo ( 'solution', 'problem' ) { print STDOUT "\n", "-" x 60, "\n\n"; print STDOUT "This is to demonstrate the $demo ...\n"; print STDOUT " Here comes the contents of the hash \%distance\n"; print STDOUT " (distances as calculated by GIS::Distance\n"; if ( $demo eq 'problem' ) { print STDOUT " and stored as that value)\n"; } elsif ( $demo eq 'solution' ) { print STDOUT " and stored as sprintf \"%25.15f\", value )\n"; } foreach my $place ( keys(%$to) ) { my $gis = GIS::Distance->new(); my $to_lat = $to->{$place}->[0]; my $to_long = $to->{$place}->[1]; my $km = $gis->distance( $from_lat, $from_long => $to_lat, $to_long, ); if ( $demo eq 'problem' ) { $distance{$place} = $km; } elsif ( $demo eq 'solution' ) { $distance{$place} = sprintf "%25.15f", $km; } print STDOUT " \$distance{$place} = $distance{$place}\n"; } print STDOUT "\n"; print STDOUT "And here comes the result of sorting the keys of that hash\n"; foreach my $place(sort {$distance{$b} <=> $distance{$a}}( keys(%distance))) { printf STDOUT "%10s is %25.15f km from here\n", $place, $distance{$place}; } }
Trying dirty trick with Imager and Inline C, but having difficulties. Can you help?
1 direct reply — Read more / Contribute
by vr
on Aug 01, 2015 at 04:00

    At some point in my program, it seems that easiest (fastest, etc.) thing to do is to directly replace raw image data with data from Perl scalar. Needless to say, Imager API (both Perl and C) don't provide for this, but I thought I'd get away with following subroutine (here it is with a test program that, of course, makes no sense):

    use strict; use warnings; use Imager; use Inline with => 'Imager'; use Inline C => << 'END_OF_C'; void _replace( Imager img, SV* str ) { unsigned char* ptr = SvPV_nolen( str ); myfree( img-> idata ); img-> idata = ptr; } END_OF_C my $i = Imager-> new( xsize => 99, ysize => 99 ); for ( 'A' .. 'F' ) { print "$_\n"; my $s; $i-> write( data => \$s, type => 'raw' ); my $j = $i-> copy(); # Here, it's rumored, some nasty things # happening, that involve $j and $s. # But maybe nothing happens. _replace( $i, $s ); } __END__ ~$ perl A B *** Error in `perl': double free or corruption (!prev): 0x0000000002d6 +7990 *** Aborted (core dumped)

    Unfortunately, my C is below-zero level. I understand I should prevent Perl from freeing memory that now is Imager responsibility, but don't know how. And, actually, replacement itself works OK. There's only error freeing memory, that happens just sometimes. This program seems to reproduce it always.

Difference between leftward and rightward list operators
2 direct replies — Read more / Contribute
by Firsov
on Aug 01, 2015 at 02:25


    I was reading perlop manual. And i don't understand one thing: What difference between rightward and leftward list operators?

    I guess that leftward list operator this is function that accept LIST (has higher precedence that comma operator). But what is a right ward list operator and why it has low precedence?

when i run a log file,will get some output in numbers say'12345' ,i would like to copy it in excel file?
2 direct replies — Read more / Contribute
by Skumar2015
on Jul 31, 2015 at 17:53
    #!/usr/local/bin/perl use strict; use warnings; my $filename = 'C:Desktop\data.log'; open(my $file1, '<:encoding(UTF-8)', $filename) or die "Could not open file '$filename' $!"; while (my $row = <$file1>) { chomp $row; print "$row\n"; } when i run this file i will get some '12345'as an output. Now I need to copy the log file output into existing excel sheet day by day. please help me.
A regex question.
2 direct replies — Read more / Contribute
by FeistyLemur
on Jul 31, 2015 at 17:49

    This is my first post here, I have a question about something I can't explain.

    I'm pretty sure I know what I did wrong, I'm just not 100% sure why it was wrong and wanted to clarify to better my understanding of regex in perl, as I'm fairly inexperienced and looking to improve.

    I was searching for vlan ids for removal in the output of "ip addr" in the following way.

    my $ethernet=`ip addr`; if ($ethernet!~/eth1\.\d{4}\@/gm){ print "No vlans exist on device.\n"; exit; } while ($ethernet=~/(eth1\.\d{4})\@/gm) { print `ip link del $1\n"; print "Removed $1\n"; }

    Doing this as above mostly worked, but it would always miss the first entry for no reason I can explain. So if there were 25 vlan entries in the string from 1001 to 1025 it would match and delete 1002-1025 without fail, and miss 1001 every time.

    Changing line 2 to:

    if ($ethernet!~/eth1\.\d{4}\@/){

    Does what I intended to do.

    I had the same problem with a similar line I was using to scrub IP addresses off the vlan devices again missing the first match, I just don't understand why the while loop breaks in the way it does because of the /gm flag on the preceeding if statements match, and was hoping someone could explain. Thanks in advance.

How to Save a variable outside of a loop
6 direct replies — Read more / Contribute
by Calebros
on Jul 31, 2015 at 17:26
    Hello everyone, I am very new coder trying to filter duplicate line records out of a .csv file by comparing two values on the current line to the previous line; however my code is returning that the variable $prev_l is uninitialized. Could someone tell me why this is happening?
    #!/usr/bin/perl use warnings; use strict; while(<>){ my $hitlength; my $prevalence; my $prev_l; my $prev_p ; my $line = $_; chomp $line; if($line =~ /^[\d]/){ my @hitline = split(/,/ , $line); $hitlength = $hitline [4]; $prevalence = $hitline [65]; if($hitlength == $prev_l and $prevalence == $prev_l){ next; } else { print "$line\n"; } $prev_l=$hitlength; $prev_p=$prevalence; } }

    Here are a couple sample lines of data
    23750,57495,78362,xxxx,2853,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 +,1,1,1,1,60,100 23751,57497,78364,xxxx,2853,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 +,1,1,1,1,60,100 23752,57500,78367,xxxx,3114,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 +,1,1,1,1,60,100 23753,57502,78369,xxxx,3114,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 +,1,1,1,1,60,100 23754,57504,78371,xxxx,101,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 +,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,60,100
remote os
2 direct replies — Read more / Contribute
by bigup401
on Jul 31, 2015 at 16:17

    is there any sample code to check remote host OS

Perl Debugger rcfile on Windows
1 direct reply — Read more / Contribute
by VinsWorldcom
on Jul 31, 2015 at 15:14

    Windows Monks - has anyone gotten the Perl Debugger rcfile (.perldb / perldb.ini) to work on Windows? I can use the Perl debugger fine with 'perl -d <progfile> [args]', but if I have a .perldb (perldb.ini) file defined, I get mixed, unsatisfying results.

    I'm on Windows 7 x64 with Strawberry 5.18.1.

    Essentially I've tried .perldb and perldb.ini in both the current directory and my home directory. It seems only perldb.ini is recognized and when it finds it, regardless of location, I get the error:

    perldb: Must not source insecure rcfile ./perldb.ini. You or the superuser must be the owner, and it must not be writable by anyone but its owner.

    So I've tried to modify permissions of the perldb.ini file on Windows by breaking the parent inheritance on that file and making sure I'm the owner and the only one with "Full Permissions" - which still doesn't fix the problem.

    Anyone get this working successfully?

    UPDATE: I've narrowed it down in

    'use vars qw($rcfile);' does a check which for Windows will return 'perldb.ini' regardless explaining why '.perldb' has no effect, despite POD about running interactive vs non- mode.

    Then, that 'perldb.ini' file is (eventually) run through "sub is_safe_file" which does a 'stat()' and on Windows, is obviously not returning the proper value to create a false when bit-masked with 022. See the function in for more details as well as POSIX::S_ISDIR() with $stat->mode values from Windows vs. Linux, which has the mode value I'm getting "33206" on Windows.

    That being said, I could do all sorts of things to fix this by editing, but is there a way / argument /something to get this to work without me monkeying with a module's code?

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!
  • 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.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (7)
    As of 2015-08-02 06:38 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found
      past polls