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
Read the directories from file
4 direct replies — Read more / Contribute
by finddata
on Mar 27, 2017 at 03:10
    inputfile: base { LOCATION:../set/projects/all/files }
    My code: my $fn="inputfile"; open ( my $fh, '<', $fn ) or die "Could not open file '$fn': $!"; while ( <$fh> ) { chomp; if (/^LOCATION:\s*(\S+)/) { $input_dir = $1; print $input_dir; } My output is as follows: ../set/projects/all/files expected one: /set/projects/all/files
    In the above code the location line has directory location.These directory location starts with .. How can i remove the .. from the directory line and read only the location of the directory
Differnece between Spreadsheet::ParseXLSX and Spreadsheet::ParseExcel
3 direct replies — Read more / Contribute
by Sonali
on Mar 27, 2017 at 02:15

    Hello PerlMonks,

    I have been trying to parse a .xls and another .xlsx file using the Spreadsheet::ParseExcel and Spreadsheet::ParseXLSX modules

    When I dump the contents of both the files into two different files, the structure of the hash generated is very different, I wanted to know what exactly is the difference between the two parsers.

    My code goes like this,

    #!/usr/local/bin/perl use warnings; use strict; use Spreadsheet::XLSX; use Spreadsheet::ParseExcel; use Data::Dumper; my $Output_FileName = 'outfile.txt'; my $Output_FileName1 = 'outfile1.txt'; my $excel = Spreadsheet::XLSX -> new ('book1.xlsx'); my $parser = Spreadsheet::ParseExcel->new(); my $workbook = $parser->parse('book2.xls'); open(my $FH, '>', $Output_FileName) or die "Could not open file '$Outp +ut_FileName' $!"; print $FH Dumper(\$excel); close $FH; open(my $FH1, '>', $Output_FileName1) or die "Could not open file '$Ou +tput_FileName1' $!"; print $FH1 Dumper(\$excel); close $FH1;

    Is there a better way to dump both the files so that I can compare the two dumped files? In my case the files were mostly full of information like font etc...

    Thank you!

Arrays & Output printing?
3 direct replies — Read more / Contribute
by coding1227
on Mar 27, 2017 at 01:21
    Hi PerlMonks,

    I'm trying to read in a 2-column file and print the contents in an organized manner. I tried using an array (see attached code) but I'm having some trouble making it work correctly. Any help with this would be incredibly appreciated! =)


    Input data:
    START
    TIME: 3
    ALT: 3.1
    TEMP: 4.3
    END

    START
    TIME: 2.6
    ALT: 8
    TEMP: 1
    END

    START
    TIME: 6.1
    ALT: 7.2
    COLOR: 43

    STRENGTH: 7

    TEMP: 9.3
    END


    Desired output:
    TIME: 3 ALT: 3.1 TEMP: 4.3
    TIME: 2.6 ALT: 8 TEMP: 1
    TIME: 6.1 ALT: 7.2 TEMP: 9.3 COLOR: 43 STRENGTH: 7


    #!/usr/bin/perl #perl -e 'while (<>){print if (/^START/../^END/);}' input.txt use strict; my @array; open(my $fh, "<", "../temp/out2.asc") or die "Failed to open file: $!\n"; while(<$fh>) { if (/^START/../^END/){ chomp; push @array, $_; } } close $fh; print join " ", @array; print "\n";
Device::SerialPort Brain Problems
4 direct replies — Read more / Contribute
by wm1ke
on Mar 26, 2017 at 20:39

    I play with perl and enjoy it but modules and installing them jut do not seem to fit with my old brain. I am trying to install Device::SerialPort on my Ubuntu box. After using cpanm I was told:

    Device::SerialPort is up to date.(1.04) mike@BlsckBox:~/ham$

    When I try to run the code I get the message :

    perl radio.pl Can't locate Device/SerialPort.pm in @INC (you may need +to install the Device::SerialPort module) (@INC contains: /etc/perl / +usr/local/lib/x86_64-linux-gnu/perl/5.22.1 /usr/local/share/perl/5.22 +.1 /usr/lib/x86_64-linux-gnu/perl5/5.22 /usr/share/perl5 /usr/lib/x86 +_64-linux-gnu/perl/5.22 /usr/share/perl/5.22 /usr/local/lib/site_perl + /usr/lib/x86_64-linux-gnu/perl-base .) at radio.pl line 6. BEGIN fai +led--compilation aborted at radio.pl line 6.

    Here is the faulting code:

    # # Control access to an ICOM radio # use vars qw( $ob $port ); use Device::SerialPort; use sigtrap qw(INT SIGINT); $ob = new Device::SerialPort ("/dev/ttyUSB0", 1);

    I get off my wheel chair and knell on the floor before the monks to help an old man who appreciates it.

Setting Timeout in Image::Grab??
1 direct reply — Read more / Contribute
by dazz
on Mar 26, 2017 at 15:35
    Hello

    I am using Image::Grab to gr1ab images off an IP Camera. That is all working OK.

    The problem I have is that the timeout for a bad connection is 3minutes but I expect to be grabbing images every 60 seconds. If there is a problem with the camera, I will end up with multiple threads running waiting for an image. I have looked at the documentation here http://search.cpan.org/~mahex/Image-Grab-1.4.2/lib/Image/Grab.pod

    I need to change the value of Timeout.

    My code looks like this:

    #get the latest image from the IP camera my $snapshot = new Image::Grab; $snapshot->timeout(5); #sets the timeout (sec) for image::grab (defau +lt is 180s) $snapshot->url("$Camera{$key}"); # create the grab object $snapshot->grab; # grab the image open (IMAGE, ">$lastsnap{$key}") || die syslog(LOG_ERR, "Couldn't ope +n file, $!"); # opens and clears th$ binmode IMAGE; # for MSDOS derivations. print IMAGE $snapshot->image; # save the image to the /var/images/Tra +ckcam folder close IMAGE or die $!;
    Perl debugger spits out the following error message:
    Can't access `timeout' field in class Image::Grab at upload-images.pl +line 79. at /usr/local/share/perl/5.18.2/Image/Grab.pm line 81. Image::Grab::AUTOLOAD('Image::Grab=HASH(0x8698344)', 5) called + at upload-images.pl line 79

    My question is how do I change the Timeout property to 5sec?

Joining elements of an array in a hash
5 direct replies — Read more / Contribute
by orangepeel1
on Mar 26, 2017 at 13:29
    I have a hash of arrays
    @array1 = ("one", "two", "three", "four", "five"); @array2 = ("banana", "pear", "apple"); my %hash = (numbers => \@array1, fruit => \@array2 );
    I would like to go through all the keys to the hash and join the elements of each array. I have tried something like this
    foreach my $group (keys %hash) { push @group, @{ $hash {$group}}; foreach (@group) { $statement = join ",", @group; } }
    However it does not join them. Does anyone have any suggestions for how I might go about this?
Log::Log4perl help in rotating file
2 direct replies — Read more / Contribute
by ravi45722
on Mar 26, 2017 at 02:56
    log4perl.rootLogger=DEBUG, LOGFILE log4perl.appender.LOGFILE=Log::Log4perl::Appender::File log4perl.appender.LOGFILE.filename=/home/GEMS/ravi/Node/logs/Script.lo +g log4perl.appender.LOGFILE.mode=append log4perl.appender.LOGFILE.layout=PatternLayout log4perl.appender.LOGFILE.layout.ConversionPattern=%d : %r : %c - %m%n +

    This is my config for the logger

    I seen

     Log::Dispatch::FileRotate

    to rotate the file. I need to rotate the file based on the date. (Every day one single file). But how to add this to my conf??

    In my conf pattern %d prints 2017/03/26 12:17:33 can I change this pattern to 2017-03-26 12:17:33.432

Need regexp to pick off second parenthetical item
2 direct replies — Read more / Contribute
by cormanaz
on Mar 25, 2017 at 20:50
    Good day Bros. I am processing email sender fields, some of which have an email address embedded in a set of parentheses following a string inside another set of parentheses. I need a regexp that will get only the former, leaving the latter with the sender name. To be clear, in the following example:
    #!/usr/bin/perl -w use strict; my $sentto = 'Fred Flintstone (US) (fredf@gmail.com)'; $sentto =~ m/\(.+?@.+?\)/; print "First: $`\nSecond: $&"
    I would like $` to contain "Fred Flintstone (US) " and $& to contain "(fredf@gmail.com)". The regexp I have in there puts " (US) " at the beginning of $&. I understand why this happens, but not how to fix it. I thought about maybe a look-back assertion for a close-paren, but I have some addresses like "Barney Rubble (barneyr@gmail.com)" so I don't think that will work. I'd like to have a regexp that will pick off the parenthetical address for both kinds of senders. Can someone suggest a solution?
    "I think computers have complicated lives very bigly. The whole age of, you know, computer has made it where nobody knows exactly what's going on." --D. Trump
How to output file data from files in an array to one singular file?
2 direct replies — Read more / Contribute
by perlnovice1900
on Mar 25, 2017 at 19:49

    Hello kind Monks, I am having difficulty pulling data from inside multiple log files and sorting that data into one singular file that consists of every file.

    I have created a perl script that can take all of the files in any directory and put the ones that are wanted into an array, (in this case I want all of the log files that start with 'secure'). I can get the files into the array no problem, but I do not know how to condense all of the data into a singular file afterwards.

    Here is my code:

    my @JanFiles; my $file; my $infile; my $JanFiles; my @files; my $dh; my @secures; my $outfile = 'secure.200601'; my $INFILE = @secures; opendir( $dh, "." ) or die "Can't open '.':$!\n"; while( $file = readdir($dh) ) { # add it to the set of files... push( @files, $file ); #print "[$file]\n"; } closedir( $dh ); # verify that @files got stuff .. print "===========================\n"; print "Verifying all file names.\n"; foreach( @files ) { print "[$_]\n"; } # Let's make a private reserve # of files that contain the word # 'secure' in file name #@secures; foreach( @files ) { if (/secure/) { if ( $_ ne "secure.200601") { push( @secures, $_ ); } } } #@secures = sort @secures; print "===========================\n"; print "Verifying only 'secure' file names.\n"; foreach( @secures ) { print "secure file = [$_]\n" } foreach( @secures ) { push (@secures, grep(/^Jan/ , @JanFiles )); } foreach( @JanFiles ) { print "JAN FILES = [$_]\n" } open (FILE, ">> $outfile") || die "Unable to open Write File! \n"; print FILE @JanFiles; close FILE; #foreach( @JanFiles ) { # print "JanFile = [$_]\n" #} exit;

    Thank you in advance. I will be here for any further needed information.

Accessing passwords in a script
4 direct replies — Read more / Contribute
by nysus
on Mar 25, 2017 at 16:12

    I have a script that needs passwords for things like mysql databases on other servers. I want to do this as painlessly and as securely as possible. So what I'm thinking is that my script would prompt for a single master password and then it would be granted access to the other passwords. I found this possible solution which explains how to use the KeePass module which seems like it might be a good route. But I wanted to get some input from the Monks who might have other good ideas. Many thanks.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks


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.