Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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
Compare array of hashes
4 direct replies — Read more / Contribute
by AnishaM
on Aug 25, 2016 at 08:20
    Hi Perl Monks, I have the below two hash of array of hashes.I need to compare these two array of hashes,each and every field needs to be compared.I cant use diff, I need to do it without using any inbuilt functions. Please help me out with this as I am really new to Perl. Thanks a lot in advance. HASH OF ARRAY OF HASHES 1:
    { 'details' => [ { 'name' => 'bbbb', 'time' => 1234, 'place' => AB }, { 'name' => 'aaaa', 'time' => 5678, 'place' => CD }, { 'name' => 'aaaa', 'time' => '91011', 'place' => EF }, { 'name' => 'aaaa', 'time' => '121314', 'place' => GH }, { 'time' => '151617', 'name' => 'cccc', 'place' => IJ }, ] };
    { 'details' => [ { 'name' => 'dddd', 'time' => 1234, 'place' => AB }, { 'name' => 'eeee', 'time' => 5678, 'place' => CD }, { 'name' => 'eeee', 'time' => '91011', 'place' => EF }, { 'name' => 'eeee', 'time' => '121314', 'place' => GH }, { 'time' => '151617', 'name' => 'cccc', 'place' => IJ }, ] };
Perl Array - What an array contains
4 direct replies — Read more / Contribute
by rahulme81
on Aug 25, 2016 at 07:51
     my $string = 'London,Paris,Madrid,Berlin';                                  

    Above string is passed to a function defined in another script where I am storing this in an array.

     my @capitals= split(/,/,$string);                                            

    Sometime $string can contain only one element as London

    I have a logic which requires me to check

    If my @capitals array contains only 'London'


    it contains all elements of my @capitals.

    Please help !!!

Use of uninitialized value $1
1 direct reply — Read more / Contribute
by Pazitiff
on Aug 25, 2016 at 07:42
    Hi to all. I have simple example:
    my $var; $var = "path_to_my_lib"; $var =~ m/lib/; say $1;
    Why i get exception - Use of uninitialized value $1 Sorry for noob question.
Check multiple array elements for a single condition
5 direct replies — Read more / Contribute
by Anonymous Monk
on Aug 25, 2016 at 06:38

    Just a quick question - is there a shorthand form of this:

    if ($array[0] == 0 && $array[1] == 0)

    A statement that would check both elements [0] and 1 for the condition ==0 without having to repeat the statement?

My PerlTidy utility is choking on non-ASCII characters, help me figure out why?
2 direct replies — Read more / Contribute
by Cody Fendant
on Aug 25, 2016 at 03:05

    I have a Perl::Tidy utility/text filter which works with my editor (BBEdit) and I've just noticed it's killing non-ASCII characters.

    The code itself is pretty simple:

    #!/usr/local/bin/perl -wn use Perl::Tidy; BEGIN { my $input_string = ""; my $output_string = ""; } $input_string .= $_; END { Perl::Tidy::perltidy( source => \$input_string, destination => \$output_string, argv => '-ce -l=80' ); print "$output_string\n"; }

    As you can see it just runs over the input file with  -n, putting it into a scalar. Then Perl::Tidys that scalar, then puts it back.

    It works in every other way just as I'd like, but when it encounters unicode characters (I've been working on some Russian text and need to recognise these chars: 'ОЕАИН') it replaces them with question marks.

    I can't add command-line flags like -CIO, that's not allowed. I've tried adding binmode STDOUT, ":utf8" and binmode STDIN, ":utf8" to the BEGIN block but that hasn't changed anything.

    Of course I can use Perl::Tidy in other ways, but I'm used to this utility and would like to get it working again in a way I can trust, it's become a habit.

Need to convert the XML into file
3 direct replies — Read more / Contribute
by aryapintu
on Aug 25, 2016 at 02:45
    I have a file in the below format.
    <mi> <mt>ProcessorOccupancyHexagon2Usage</mt> <mt>ProcessorOccupancyHexagon2Avg</mt> <mt>ProcessorOccupancyHexagon2Max</mt> <mv> <moid>cpuId=0</moid> <r>15.5051</r> <r>15.6304</r> <r>39.1236</r> </mv> </mi> <mi> <mt>VS.numberOfDlSrbSduReceived</mt> <mt>VS.numberOfDlSrbPduTransmitted</mt> <mt>VS.numberOfSrbDeliveryIndSent</mt> <mt>VS.numberOfDlSrbPduDropNackRecvd</mt> <mt>VS.numberOfDlSrbSduDiscarded</mt> <mt>VS.numberOfSrbUlPduReceived</mt> <mt>VS.numberOfSrbUlSduTransmitted</mt> <mt>VS.numberOfUlSrbSduDropped</mt> <mv> <moid>plmnId=26290,HenBId=768012,crnti=132,srbId=1</moid> <r>53</r> <r>53</r> <r>22</r> <r>0</r> <r>0</r> <r>63</r> <r>63</r> <r>0</r> </mv> <mv> <moid>plmnId=26290,HenBId=768012,crnti=132,srbId=2</moid> <r>30</r> <r>30</r> <r>30</r> <r>0</r> <r>0</r> <r>52</r> <r>52</r> <r>0</r> </mv> </mi>
    the above file need to be converted in to the below format.
    cpuId=0 ProcessorOccupancyHexagon2Usage 15.5051 ProcessorOccupancyHexagon2Avg 15.6304 ProcessorOccupancyHexagon2Max 39.1236 plmnId=26290,HenBId=768012,crnti=132,srbId=1 VS.numberOfDlSrbSduReceived 53 VS.numberOfDlSrbPduTransmitted 53 VS.numberOfSrbDeliveryIndSent 22 VS.numberOfDlSrbPduDropNackRecvd 0 VS.numberOfDlSrbSduDiscarded 0 VS.numberOfSrbUlPduReceived 63 VS.numberOfSrbUlSduTransmitted 63 VS.numberOfUlSrbSduDropped 0 plmnId=26290,HenBId=768012,crnti=132,srbId=2 VS.numberOfDlSrbSduReceived 30 VS.numberOfDlSrbPduTransmitted 30 VS.numberOfSrbDeliveryIndSent 30 VS.numberOfDlSrbPduDropNackRecvd 0 VS.numberOfDlSrbSduDiscarded 0 VS.numberOfSrbUlPduReceived 52 VS.numberOfSrbUlSduTransmitted 52 VS.numberOfUlSrbSduDropped 0
How to give input to terminal (command window) in perl
3 direct replies — Read more / Contribute
by tsathish123
on Aug 25, 2016 at 01:32

    Below is my question

    I start the process in Linux. Give input through terminal or command window to the process to do different set of actions.

    I would like to automate the same by using perl script.

    Like start the particular process. After process started, I would like to give input to the process like i give through keyboard to terminal.

    Since i am a beginner to perl language. Please help me by giving ideas or example code snippet.

Mp4 editing
2 direct replies — Read more / Contribute
by palkia
on Aug 24, 2016 at 21:50
    Hello everyone.

    As the title hints, I would like to break apart mp4 videos to animation and sound, so these may be edited, and recombined, to form an edited version of the mp4 file.
    My question is: do you know of any good and reliable modules that can help me do so ?

    Thank you very much for any assistance ☺
howto parse (or determining end) of a line of perl
5 direct replies — Read more / Contribute
by perl-diddler
on Aug 24, 2016 at 20:26
    I have a little calc I wrote in perl that really just provides and eval & print loop for the user.

    I have made changes over the years, but one of the things that has always bothered me is, if I want to add a complex expression -- anything that is multi-lined, how to get my input loop to know when it needs more input OR when it doesn't (vs. _could_ take more input).

    My 'semi-model' for that would be something like bash, where if you type 'the beginning of a control structure, bash will change to a different prompt to indicate it wants more input.

    How might I do the same in my eval/print loop?

    As it is now, I can define functions on 1 line, for example, but there is no easy way to extend that to more than one line.

    I could force the use of an 'extend char', like backslash at the end of line -- but in bash, those are only needed if it is ambiguous -- i.e. if the line is already well formed, you need to enter '\' to tell bash to keep parsing. Ex. (using 'home>' as normal prompt):

    home> int a=1+1\ > +2; echo $a 4
    You can't enter partial *expressions* in bash and have it "auto continue" (that I know of). I.e.
    home> a=1+<cr> -bash: 1+: syntax error: operand expected (error token is "+")
    But I could enter a '\' at the end of line and continue it as I did above.

    Where bash works to auto-detect is in its control structures (or like if a quote is still open). Ex:

    home> for ((i=0;i<10;++i)); then<cr> > [...]
    On the 2nd line, it doesn't display the normal prompt, but a single greater than sign. How could I get my input/eval loop to get feedback from perl that I'm in the middle of a similar structure and change the prompt and not try to eval it?


    Clarification: how can I do that w/o writing an entire perl parser? ;-)

Captured single quote results in captured phantom quote
3 direct replies — Read more / Contribute
by Ach
on Aug 24, 2016 at 17:21

    This appears to be a bug in v5.22.2 built for cygwin-thread-multi:

    DB<1> $line = "\"quoted-text\" \cM\cJ" DB<2> x $line 0 "\"quoted-text\" \cM\cJ"

    My original regex:

    DB<3> x $line =~ m/^(\s*)((?'quote'"?)[^\s"]*\g{quote})\s+(\[[^\]]*\ +]|)\s*$/ 0 '' 1 '"quoted-text"' 2 '"' 3 ''

    Remove backreference:

    DB<4> x $line =~ m/^(\s*)((?'quote'"?)[^\s"]*")\s+(\[[^\]]*\]|)\s*$/ 0 '' 1 '"quoted-text"' 2 '"' 3 ''

    Replaced named with unnamed group:

    DB<5> x $line =~ m/^(\s*)(("?)[^\s"]*")\s+(\[[^\]]*\]|)\s*$/ 0 '' 1 '"quoted-text"' 2 '"' 3 ''

    Replace unnamed group with internals of group:

    DB<6> x $line =~ m/^(\s*)("?[^\s"]*")\s+(\[[^\]]*\]|)\s*$/ 0 '' 1 '"quoted-text"' 2 '' 3 ''

    Needless to say, the commands 2-5 resulted in the wrong result for array element 2.

    Has this been discovered and fixed yet?



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.
  • 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 taking refuge in the Monastery: (6)
    As of 2016-08-29 07:08 GMT
    Find Nodes?
      Voting Booth?
      The best thing I ever won in a lottery was:

      Results (399 votes). Check out past polls.