http://www.perlmonks.org?node_id=479

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
Trying to use old source code
6 direct replies — Read more / Contribute
by Radude
on Jul 23, 2018 at 08:55

    Newbie here. I got my hands on some old source code I'd like to use and judging from my research it may be too old to just use. I may have to rewrite everything. Am I correct in thinking this? Here's the beginning of the code...

    #!/usr/bin/perl require 5.005; BEGIN { $| = 1; } use strict; use bytes; #open (EL, '>>rocklog.txt') || die "Can't open rocklog.txt: $!\n"; sel +ect(EL); $|=1; require('r2inter.pl'); use webinter; use IO::Socket; use IO::Select; use Data::Dumper; use Benchmark;
Comparing two files line by line and exporting the differences from the first file
3 direct replies — Read more / Contribute
by jzelkowsz
on Jul 22, 2018 at 20:49

    I have two files. One is an HR record of the user's values; the other is a network export of their attributes. I am trying to compare the two files and find the differences attribute by attribute. The sole reliable key is the samaccountname which is present and consistent in every record. I am trying to produce a file like this:

    barsu991,title,Director of Cooks zingk072,symphonyemployeetype,IKP zingk072,employeenumber,zingk072 zingk072,manager,"cn=manager1,ou=users,ou=Kitchen,dc=Kitchen,dc=net"
    Where each line of the produced file holds the samaccountname,attribute that is incorrect, and the correct value of the attribute from the HR record. One mistake per line.

    I have tried to do this with loops like below but the best I get is a comparison with the last line and not all of them.

    open(HR, "<hr.txt") || die "can't open hr"; open(AD, "<ad.txt") || die "can't open ad"; open(COMMAND, ">com.txt") || die "can't open com.txt"; while(<HR>) { ($samaccountnameHR,$givennameHR,$snHR,$initialsHR, $employeenumberHR,$symphonyemployeetypeHR,$mailHR, $titleHR,$departmentHR,$companyHR,$lHR, $physicaldeliveryofficeHR,$streetaddressHR,$stHR, $postalcodeHR,$telephonenumberHR,$managerHR)=split(/,$/); while(<AD>) { ($samaccountnameAD,$givennameAD,$snAD,$initialsAD,$employeenumberAD, $symphonyemployeetypeAD,$mailAD, $titleAD,$departmentAD,$companyAD, $lAD,$physicaldeliveryofficeAD,$streetaddressAD,$stAD,$postalcodeAD, $telephonenumberAD,$managerAD)=split(/,$/); if ($employeenumberHR != $employeenumberAD) { print "$samaccountnameHR $samaccountnameAD\n"; } } }
    HR Data: samaccountname,givenname,sn,initials,employeenumber, symphonyemployeetype,mail,title,department,company,l, physicaldeliveryoffice,streetaddress,st,postalcode, telephonenumber,manager barsu991,Uttiam,Barski,K,20114598,IKP, Uttiam.Barski@pulse.org,Director of Cooks,Day Kitchen, MILIFO,Alpena,Kitchen of the World,400 Baker,WI,50987, 555-555-5555,"cn=manager1,ou=users,ou=Kitchen,dc=Kitchen,dc=net" walkl003,Lreblemet,Walker,J,20178941,IKP, Lreblemet.Walker@pulse.org,Head Cook,Day Kitchen,MILIFO,Alpena, Kitchen of the World,400 Baker,WI,50987,555-555-5551, "cn=manager1,ou=users,ou=Kitchen,dc=Kitchen,dc=net" karss001,Sovyetk,Karsten,Y,20146598,IKP,Sovyetk.Karsten@pulse.org, Dishwasher,Day Kitchen,MILIFO,Alpena,Kitchen of the World, 205 Willy B. Temple,WI,50987,, "cn=manager1,ou=users,ou=Kitchen,dc=Kitchen,dc=net" zingk072,Kovon,Zingerman,K,20113578,IKP,Kovon.Zingerman@pulse.org, Baker,Day Kitchen,MILIFO,Alpena,Kitchen of the World, 205 Willy B. Temple,WI,50987,, "cn=manager1,ou=users,ou=Kitchen,dc=Kitchen,dc=net" peizs194,Synthia,Smite,B,20134743,IKP,Synthia.Peizer@pulse.org, Broiler Man,Day Kitchen,MILIFO,Alpena, Kitchen of the World,205 Willy B. Temple, WI,50987,,"cn=manager1,ou=users,ou=Kitchen,dc=Kitchen,dc=net" hutcy231,Yello,Hutchinson,W,20145712,IKP, Yello Hutchinson,@pulse.org, Bottle Washer,Day Kitchen,MILIFO,Alpena, Kitchen of the World,400 Baker,WI,50987, ,"cn=manager1,ou=users,ou=Kitchen,dc=Kitchen,dc=net" haserz221,Zebediah,Haserkrilk,L,20125471,IKP, Zebediah.Haserkrilk@kit.org, Purchaser,Day Kitchen,MILIFO,Alpena, Kitchen of the World,400 Baker,WI,50987, ,"cn=manager1,ou=users,ou=Kitchen,dc=Kitchen,dc=net"
    AD data: samaccountname,givenname,sn,initials,employeenumber, symphonyemployeetype,mail,title,department,company,l, physicaldeliveryoffice,streetaddress,st,postalcode, telephonenumber,manager barsu991,Uttiam,Barski,K,20114598,IKP, William.Barski@pulse.org,Chief of Cooks,Day Kitchen, MILIFO,Alpena,Kitchen of the World,400 Baker,WI,50987, 555-555-5555, "cn=manager1,ou=users,ou=Kitchen,dc=Kitchen,dc=net" walkl003,Larry,Walker,J,,IKP,Larry.Walker@pulse.org, Cook,Day Kitchen,MILIFO,Alpena,Kitchen of the World, 400 Baker,WI,50987,555-555-5551, "cn=manager1,ou=users,ou=Kitchen,dc=Kitchen,dc=net" karss001,Steven,Karsten,Y,20146598,IKP, Steven.Karsten@pulse.org,Dishw,Day Kitchen,MILIFO, Alpena,Sully's Kitchen,48720 Belcard,IL,34567,, "cn=manager1,ou=users,ou=Kitchen,dc=Kitchen,dc=net" zingk072,Kevin,Zingerman,K,,,Kevin.Zingerman@pulse.org, Baker,Day Kitchen,MILIFO,Alpena,Kitchen of the World, 205 Willy B. Temple,WI,50987,, peizs194,Samantha,Smith,B,20134743,IKP, Samantha.Smith@pulse.org,"Man, Broiler",Day Kitchen, MILIFO,Alpena,Kitchen of the World,205 Willy B. Temple, WI,50987,,"cn=manager1,ou=users,ou=Kitchen,dc=Kitchen,dc=net" hutcy231,Yaren,Hutchinson,W,20145712,IKP, Yaren Hutchinson,@pulse.org,Bottle Washer,Day Kitchen,MILIFO, Alpena,Kitchen of the World,400 Baker,WI,50987,, "cn=manager1,ou=users,ou=Kitchen,dc=Kitchen,dc=net" haserz221,Zebediah,Hasermann,L,,IKP, Zebediah.Haserman@pulse.org,Purchaser,Day Kitchen,MILIFO, Alpena,Kitchen of the World,400 Baker,WI,50987,555-555-5555, "cn=manager1,ou=users,ou=Kitchen,dc=Kitchen,dc=net"

sub_job in jjobs
2 direct replies — Read more / Contribute
by dideod.yang
on Jul 22, 2018 at 20:29
    Hi Monks :) I always keep thank for monks's nice idae. Do you know jjobs command? Below code, there are jjobs command show me in linux screen. If you know about below command or jjobs, Do you have nice idea to cath JOBID finished? When job finished, JOBID is deleted. Of course I can cath JOBID for every seconds to detact finished JOBID. but that way is not nice I think.. Do monks have nice idea? or special function to catch that JOBID?
    JOBID USER STAT QUEUE FROM_HOST EXEC_HOST JOB_NAME + SUBMIT_TIME APPLICATION 9874211 dideod RUN gui linux test *vevue.il" Jun 2 +7 10:17 test 5507578 dideod RUN gui linux test *vevue.il" Jul +6 11:12 test
Multiple assignment in Perl
1 direct reply — Read more / Contribute
by rnaeye
on Jul 22, 2018 at 19:27

    In Python I can do the following:

    a, b = 0, 1 while b < 50: print(a, b) a, b = b, a + b

    That is shorter version of the following assignment (I think they call it atomic version):

    old_a = a a = b b = old_a + b

    How do I do the first assignment in Perl? My Perl version is

    use warnings; use 5.18.2; my ($a, $b) = (0, 1); while ($b < 50) { say "$a $b"; my $old_a = $a; $a = $b; $b = $old_a + $b; }

    Thank you.

AIX harddisk sum of storage
3 direct replies — Read more / Contribute
by pjzero@90
on Jul 22, 2018 at 13:21

    Hi perl monks, I'm trying to go through 250 aix lpars and capture. I SSH into each host and attempting to run a for loop within backticks, come to find out this does not work, it fails when it gets to "do" Any ideas or workarouns? Thanks.

    sub aixstorage { my $all_servers = 'path to my AIX list of servers'; my @hosts=`cat $all_servers`; my $filename = '/tmp/aixstorage_report.rtf'; foreach my $one (@hosts){ chomp $one; my $ping = `ping -c 1 -w 1 $one 1>/dev/null 2>&1`; if (defined ($ping)) { my $SSH ="/usr/bin/ssh -o ConnectTimeout=3 -o Stri +ctHostKeyChecking=no"; my $space = $SSH root\@$one for i in lspv | awk '{pri +nt \$1}'; do bootinfo -s \$i; done | awk '{sum+=\$1}END{print (sum\/1 +024)}'}'"`; print "$space\n"; }else { print "\nATTENTION: $one is offline\n" } } }
When did perl change /bin wrappers?
1 direct reply — Read more / Contribute
by Anonymous Monk
on Jul 22, 2018 at 07:48
    /usr/bin/corelist used to be a wrapper for (example) /usr/bin/corelist5.18 but now the wrapper is the numbered program itself. When did that change? Thank you
how to stop command
2 direct replies — Read more / Contribute
by dideod.yang
on Jul 22, 2018 at 05:51
    Hi monks. I wonder how to stop command and keep operating script??. I have many files that I want to search for 5 seconds. Below script,I use alarm but I can not keep my script because of die. If I erase die there is no reason to use alarm.. right? please help me
    my $timeout = 5; eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm $timeout; my @file_list = `ls user/test/`; alarm 0; }; if ($@) { die unless $@ eq "alarm\n"; # propagate unexpected errors # timed out } else { # didn't } # I want to keep go scrip after stop alarm my $file; my @file_list; foreach $file(@file_list){ print "$file\n"; }
option control in script
3 direct replies — Read more / Contribute
by dideod.yang
on Jul 22, 2018 at 00:52
    Hi monks. I wonder that Can script control option by itself?? For example, when I want to use @F(automatic split line by line) , I operate option -ane "perl -ane script.pl". also there are many options such as -n -e ... Recently I write some script to open text file and really useful on @F. but I always forgot option -ane :(.. I know I am not man.. So I need your help. When I operate script "perl script.pl" then script is operated on "-ane" to use @F. Thank you :)
Counting characters without a space
2 direct replies — Read more / Contribute
by htmanning
on Jul 21, 2018 at 21:52
    I have a form that users are filling in by listing numbers separated by commas, but not adding a space. This results in a very long string which makes the resulting page too wide. My thought it to count the number of characters in each word and target any word more than 15 characters without a space. I would then replace the commas with a comma and space. Anyone have a better way to do it?

    How do I find words that are longer than 15 characters?

Perl/Tk Windows fonts for Linux?
1 direct reply — Read more / Contribute
by tlhackque
on Jul 21, 2018 at 16:08

    I think I have all the pieces, but not how to put them together:

    I have a Perl/Tk application that runs on Windows & Linux (Fedora, if it matters). Originally written on Windows.

    The Linux default fonts are causing some issues - they make a number of widgets larger, and aren't as readable for some. In any case, the goal is a common look and screen size.

    The application users (X display servers) are mostly Windows (VxXsrv), some Linux.

    So, I'd like to have the application request (and the clients use) the Windows fonts for the Tk widgets.

    What I (think I) know:

    • Perl Tk has "Tk Default, Text, Fixed, Monospace, Heading, Caption, Small Caption, Icon and Tooltip fonts.
    • On Windows, the "System" font is Arial, which is used for the widgets without custom fonts
    • The MS TT fonts (including Arial) can be obtained for use using the process at corefonts.sourceforge.net
    • There ought to be some mapping in .Xdefaults to connect the dots

    That's where I get lost.

    • A - having followed the process (built and installed the RPM), xlsfonts doesn't report Arial on Linux clients. It does report a bunch of other -microsoft fonts. xlsfonts | grep ^-microsoft | sort | head starts with -microsoft-comic
    • B - I assume I need to do something to get VcXsrv to recognize the fonts too. I have no clue what.
    • C - Even if it did - what do I put in .Xdefaults (or elsewhere) to have TK adopt the MS Arial (and Courier) fonts?

    I don't want to declare a custom font for every widget - I want the defaults for this application to switch to these as the "system" fonts"

    I don't know why I find all this stuff so confusing - OS internals are simple :-)

    Thanks in advance


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!
  • 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.