Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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
Unable to remove items from an array that are empty ' '
3 direct replies — Read more / Contribute
by nat47
on Apr 25, 2015 at 17:06
    When I use Data Dumper to view my array I see this

    $VAR1 = 'stuff'; $VAR2 = ''; $VAR3 = 'stuff'; $VAR4 = ''; $VAR5 = '10'; $VAR6 = ''; $VAR7 = '-'; $VAR8 = '';
    This is how I tried to remove it, and it didn't work:
    do { $a_counter = 0; foreach my $x(@left_split) { if($x eq '') { splice @left_split, $a_counter-1, 1; $bool = 1; last; } } }
Net::SSH2 channel returns no output
1 direct reply — Read more / Contribute
by rama133101
on Apr 25, 2015 at 16:08

    I need to send some commands to remote linux box and fetch responses. I am using Net:SSH2 for creating a session channel and send commands using shell.

    The problem I face is that the response I receive is empty. If I add a sleep statement, the output is very well captured. I do not want to add sleep statements as I need to send multiple commands and that would reduce the performance. Please advice what I am missing.

    Here is the code.

    $session = Net::SSH2->new(); $rc = $session->connect($target_ip, $target_port, Timeout=>4000) ; print "\n rc: $rc"; $session->auth_password($username, $passwd) ; $chan = $session->channel(); $chan->shell() ; print $chan $cmd . " \n" ; my @poll = ({handle=>$chan, events=>['in', 'ext', 'channel_closed']}); $session->blocking(0) ; $session->poll(1, \@poll) ; if ($poll[0]->{revents}->{in}) { while (<$chan>) { $resp .= $_ ; } } print "\nresponse : $resp";
Moose attribute modification
2 direct replies — Read more / Contribute
by learning.moose
on Apr 25, 2015 at 12:41

    Hello monks.

    I have a class containing text attribute, but I don't want to create printText method inside that class. I would rather create separate object that will manage my print and formatting.

    This is where the problem starts. I don't want inheritance, would rather do it with roles. How can I make role read/edit fields (attributes) in a class directly, so I can do it with a code like this:

    my $text = TextClass->new( text => 'some text'); $text->interfacePrint->methodPrintWithinInterface();

    I dont want to pass $text->text attribute directly.

    $text->interfacePrint->methodPrintWithinInterface( $text->text );

    ^ This is how I dont want to do it

How would you solve a user-inputted simple one-variable Linear Equation
5 direct replies — Read more / Contribute
by nat47
on Apr 25, 2015 at 12:27
    What is the process that you would use to go about solving a user-inputted linear equation? Only for simple one variable equations such as

    10x+4=7

    3(x+9)=8x

    Just following the basic math rules for solving linear equations? That's going to be a LOT of if/else statements will it not? Would it be best to break apart the input equation into two parts, left side and right side? And then go through rules on each side of the equation (distribution) etc, and then multiply/divide each side to get x by itself?

    I really want to do this is code, not using a module that's already been programmed to do this.

Wait for individual sub processes
6 direct replies — Read more / Contribute
by crackerjack.tej
on Apr 25, 2015 at 03:01

    Dear monks,

    I am essentially writing a Perl script that divides a large input file for a text processing tool, so that I can process the files faster. I am working on a CentOS 6 based cluster, where each CPU has 16 cores. My idea is to split the input file into 16 parts, and run 16 instances of the text processing tool, and once all of them are done, I parse the output and merge it into a single file. In addition, the script will continue to process the next input file in a similar way. I have achieved that using fork(), wait() and exec() as follows (Omitting code that is not relevant):

    use strict; use warnings; use POSIX ":sys_wait_h"; #Split input files into parts and store the filenames into array @ +parts ... my %children; foreach my $part (@parts) { my $pid = fork(); die "Cannot fork for $part\n" unless defined $pid; if ($pid == 0) { exec("sh text_tool $part > $part.out") or die "Cannot exec + $part\n"; } print STDERR "Started processing $part with $pid at ".localtim +e."\n"; $children{$pid} = $part; } while(%children) { my $pid = wait(); die "$!\n" if $pid < 1; my $part = delete($children{$pid}); print STDERR "Finished processing $part at ".localtime."\n"; }

    While I got what I wanted, there is a small problem. Due to the nature of the text processing tool, some parts get completed much before others, in no specific order. The difference is in hours, which means that many cores of the CPU are idle for a long time, just waiting for few parts to finish.

    This is where I need help. I want to keep checking which part (or corresponding process) has exited successfully, so that I can start the processing of the same part of the next input file. I need your wisdom on how I can achieve this. I tried searching a lot on various forums, but did not understand correctly how this can be done.

    Thanks.

    ------UPDATE---------

    Using a hash, I can now find out which process is exiting when. But I fail to understand how to use this code in an if block, so that I can start the next process. Can someone help me with that? I have updated the code accordingly.

    ----------------UPDATE 2--------------

    I guess it's working now. Using Parallel::ForkManager, and a hash of arrays that stores the pids of each input file, I am able to track the sub processes of each file separately. By maintaining a count of number of subprocesses exited, I can call the sub for output parsing as soon as the count reaches 16 for an input file. I will come back if I run into any other problem.

    Thanks a lot for all the help :)

    P.S. Is there any flag that I have to set that this thread is answered/solved?

Getting croak to show both caller and callee?
5 direct replies — Read more / Contribute
by Anonymous Monk
on Apr 25, 2015 at 01:21
    Hi, friends. So we use "croak" all the time. We have some code though that does some dynamic dispatching, and the "croak" shortmess (only showing the caller origin) is inadequate. All we see is the error happening in the "dispatch" routine, but we really need to see where it was dispatched *to*. Of course, just using "die" in this situation work fine; it shows the actual location of the croak. But we since we use "croak" so often, we screw it up sometimes and use croak rather than die. So my question is: is there a way to coax Carp::croak into showing *both* the caller and the callee of the croak call? Another alternative is to use $Carp::Verbose, so we get a full backtrace -- that does indeed work too. Just that it also generates a huge callstack that we'd rather not see on every exception. If Carp::croak cannot be convinced to do this, any suggestions? Maybe write our own "croak" routine that uses Carp's shortmess()?
Six way diff? (Solved. Low-tech rules!)
4 direct replies — Read more / Contribute
by BrowserUk
on Apr 24, 2015 at 18:39

    I have six chunks of 52 lines of very similar code that I need to isolate all the differences in.

    I've been tackling it by eye, and have it mostly working, but there remains some small difference that I'm not seeing; and I'm going bug-eyed for looking.

    Anyone any thoughts how to do a six-way diff?


    Everything below is update.

    I settled for a low tech solution that worked surprisingly well.

    First, I put the six chunks of code into separate files. Then I wrote the following simple Perl script that loaded the lines of those files in to an AoAs: all the line 1s in to one array, all line 2s in to another; and so on; and then printed them to standard out:

    #! perl -slw use strict; use Data::Dump qw[ pp ]; my @lines; for my $file ( qw[ x1.js x2.js y1.js y2.js z1.js z2.js ] ){ open FH, '<', $file or die $!; push @{ $lines[ $. ] }, $_ while <FH>; close FH; } for my $l ( @lines ) { next unless defined $l; print @$l; }

    What I got after normalising the files and lines -- which was surprisingly quick by referencing the output -- was stuff that looked like this:

    E:\Chart>diff6 | more gg = _GetGrid( this.Min.y / this.Parent.Zoom.y, this.Max.y / this.Pare +nt.Zoom.y, this.Scale.y ); gg = _GetGrid( this.Min.z / this.Parent.Zoom.z, this.Max.z / this.Pare +nt.Zoom.z, this.Scale.z ); gg = _GetGrid( this.Min.x / this.Parent.Zoom.x, this.Max.x / this.Pare +nt.Zoom.x, this.Scale.x ); gg = _GetGrid( this.Min.z / this.Parent.Zoom.z, this.Max.z / this.Pare +nt.Zoom.z, this.Scale.z ); gg = _GetGrid( this.Min.x / this.Parent.Zoom.x, this.Max.x / this.Pare +nt.Zoom.x, this.Scale.x ); gg = _GetGrid( this.Min.y / this.Parent.Zoom.y, this.Max.y / this.Pare +nt.Zoom.y, this.Scale.y ); if( this.GridDelta.y != 0 ) gg[1] = this.GridDelta.y; if( this.GridDelta.z != 0 ) gg[1] = this.GridDelta.z; if( this.GridDelta.x != 0 ) gg[1] = this.GridDelta.x; if( this.GridDelta.z != 0 ) gg[1] = this.GridDelta.z; if( this.GridDelta.x != 0 ) gg[1] = this.GridDelta.x; if( this.GridDelta.y != 0 ) gg[1] = this.GridDelta.y; var ii = 0; var ii = 0; var ii = 0; var ii = 0; var ii = 0; var ii = 0; for( var jj = gg[2]; jj >= gg[0]; jj -= gg[1] ) { for( var jj = gg[2]; jj >= gg[0]; jj -= gg[1] ) { for( var jj = gg[2]; jj >= gg[0]; jj -= gg[1] ) { for( var jj = gg[2]; jj >= gg[0]; jj -= gg[1] ) { for( var jj = gg[2]; jj >= gg[0]; jj -= gg[1] ) { for( var jj = gg[2]; jj >= gg[0]; jj -= gg[1] ) { pp.y = jj * this.Parent.Zoom.y; pp.z = jj * this.Parent.Zoom.z; pp.x = jj * this.Parent.Zoom.x; pp.z = jj * this.Parent.Zoom.z; pp.x = jj * this.Parent.Zoom.x; pp.y = jj * this.Parent.Zoom.y; pp.z = this.Min.z; pp.y = this.Min.y; pp.z = this.Min.z; pp.x = this.Min.x; pp.y = this.Min.y; pp.x = this.Min.x; var vv = this.Parent.ScreenPos( pp ); var vv = this.Parent.ScreenPos( pp ); var vv = this.Parent.ScreenPos( pp ); var vv = this.Parent.ScreenPos( pp ); var vv = this.Parent.ScreenPos( pp ); var vv = this.Parent.ScreenPos( pp ); fromOV( this.Line[4][ii], vv.x, vv.y ); fromOV( this.Line[5][ii], vv.x, vv.y ); fromOV( this.Line[2][ii], vv.x, vv.y ); fromOV( this.Line[3][ii], vv.x, vv.y ); fromOV( this.Line[0][ii], vv.x, vv.y ); fromOV( this.Line[1][ii], vv.x, vv.y ); ...

    I piped that into a file, wrapped it into a subroutine and eliminated the duplicates, then went through the sets of lines with differences and parameterised them one at a time. The result is:

    Where first six lines are the call sites and the rest the fully parameterised function.

    And the best bit: IT WORKS!

    The names need work, but that's true for all the names in the library. And a job for tomorrow.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
trying to format a phone list
5 direct replies — Read more / Contribute
by sonikd
on Apr 24, 2015 at 13:09

    hello Perlmonks, i have a phone list in a text file, the names and phone numbers are a bit jumbled up, i am wanting a script to sort them like this:

    Black, Joe (333)-321-0987

    Smith, Sue (333)-321-0534

    Brown, Andy (333)-587-0986

    Currently, they look something like this:

    Black, Joe 0987

    Smith, Sue 0534

    Brown, Andy 587-0986

    the area code on all numbers in the txt file will need to be (333) for example, and the exchange prefix is assumed to be 321 unless otherwise specified, such as the one example that shows 587 as the exchange prefix.

    The names and numbers need to list in ascending alpha order by last name, no quotation marks, even columns, inserts area code as (333) unless otherwise marked, dashes between area code and exchange prefix and between line number

    i found another post on here which would allow me to properly format the phone numbers, but i have no idea how i would incorporate that in a script or write a new script that would allow me to format an entire txt file and output it to another text file with the names/numbers correctly formatted. Thanks in advance for any help.

    heres the other code i found:

    #!/perl/bin/perl use strict; use warnings; # The assumption is that the number will always be exactly ten digits my $s = "0123456789"; print $s, "\n"; $s =~ s/^(\d{3})(\d{3})(\d{4})$/($1)$2-$3/; print $s, "\n"; __OUTPUT__ 0123456789 (012)345-6789
Compare elements from two arrays and count
2 direct replies — Read more / Contribute
by skalman
on Apr 24, 2015 at 10:39

    Hi Im extremely new to Perl and need help with this problem. I have two arrays that consists of usernames. One of the arrays only consists of one username of each and the second one consists of multiples of the same usernames fond i array1. How to compare these two arrays and count how many times each element is found in array two. I allso want to print the results Example output

    user1:1 user2:3 user3:9
error on use Text::Table;
2 direct replies — Read more / Contribute
by toto
on Apr 24, 2015 at 09:15
    Can't locate Text/Table.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.12.4 /usr/local/share/perl/5.12.4 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.12 /usr/share/perl/5.12 /usr/local/lib/site_perl .) at ./tableau_perl.pl line 5.

Add your question
Title:
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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    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 contemplating the Monastery: (5)
    As of 2015-04-26 00:25 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Who makes your decisions?







      Results (481 votes), past polls