Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

The Monastery Gates

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

Donations gladly accepted

If you're new here please read PerlMonks FAQ
and Create a new user.

New Questions
Parsing .txt into arrays
4 direct replies — Read more / Contribute
by Fshah
on May 24, 2017 at 05:32

    Hi there, I have a large log file in .txt format which has several pages (each page has different format,with different colums pertaining to different fields) I want to separate each column into an array so that it can be used to process later ,how do I do this I have a picture of how log file looks let me know if u need it, Thanks in advance

    place and year data: 67 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |no.| name | age | place | year | |_ _|_ _ _ _|_ _ _ | _ _ _ | _ _ | |1 | sue |33 | NY | 2015 | |2 | mark |28 | cal | 2106 | work and language :65 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |no.| name | languages | proficiency | time taken| |_ _| _ _ _| _ _ _ _ _ |_ _ _ _ _ _ _| _ _ _ _ _ | |1 | eliz | English | good | 24 hrs | |2 | susan| Spanish | good | 13 hrs | |3 | danny| Italian | decent | 21 hrs | Position log | | |Pos |value | |bulk|lot| prev| newest| |# |Locker|(dfg) |(no) |nul|val |Id | val |val | ----------------------------------------------------------- | 0| 1| 302832| -11.88| 1| 0|Pri| 16| 0| | 1| 9| 302836| 11.88| 9| 0|Pri| 10| 0| | 2| 1| 302832| -11.88| 5| 3|Pri| 14| 4| | 3| 3| 302833| 11.88| 1| 0|sec| 12| 0| | 4| 6| 302837| -11.88| 1| 0|Pri| 16| 3|

    I want these columns into an array with name as given in the table containing values as in the table. thank you.

Wildcards for a wide array of strings?
7 direct replies — Read more / Contribute
by zarath
on May 23, 2017 at 04:01
    Heyhey!

    Perl-virgin here, be gentle, pretty please with sugar on top. + English is not my native tongue, but i pride myself in my capabilities (in written English anyways, I sound a bit like a mix of a Frenchman and a German trying to speak American).

    I have a question about the very first script I'm trying to write.

    I'm sure this will be resolved in a jiffy by the first person who bothers to read this whole thing. Will you be that person?

why is the size of notebooks area smaller than main window
2 direct replies — Read more / Contribute
by madhusudhan_s
on May 22, 2017 at 00:49

    Hi,

    I am trying to write a program which has many notebooks and i found a reference code from the web as below which is exactly what i want. When i make the main window size 870x1000 i notice that the area where the notebooks are present is smaller than the main window. How should i modify this code such that the notebooks are as big as the main window?
    #!/usr/bin/perl use strict; use warnings; use Tk; use Tk::NoteBook; our $mw = MainWindow->new; $mw->geometry("870x1000"); my $canvas = $mw->Scrolled('Canvas',-bg=>'red')->pack; our $nb = $canvas->NoteBook; ### add 20 tabs to notebook for ( 1 .. 20 ){ my $title = "Untitled ($_)"; $nb->add( $_, -label => $title ); } ### embed the notebook widget into the canvas my $nb_in_canvas = $canvas->createWindow( 0,0,-window => $nb,-anchor = +> 'nw' ); $canvas->update; ### the whole notebook is larger than what can be initially displayed ### so we'll have to sroll it later on in the program $canvas->configure(-scrollregion => [0,0,$nb->reqwidth,$nb->reqheight] +); MainLoop;
John Guttag's book - 2nd exercise. My attempt in Perl.
11 direct replies — Read more / Contribute
by pritesh_ugrankar
on May 20, 2017 at 16:22

    Hi Monks,

    I came across John Guttag's book on computation - "Introduction to Computation and Programming Using Python". Note that I love Perl, but found this book to be very interesting. I tried the second finger exercise and though, initially it appeared trivial to solve, it took me a real long time to figure it out.

    Here's the exercise: Write a program that examines three variables— x , y , and z — and prints the largest odd number among them. If none of them are odd, it should print a message to that effect.

    Points to note:- This is to be tackled using only if/else loop and comparisons, because that is what the author has covered till that part.

    Here's my attempt in Perl. I tried other ways, but most of them would not print the expected results if either of the numbers were negative, or two of the three numbers were even.

    use strict; use warnings; my ($x, $y, $z) = (-11,-13,4); if ($x % 2 == 0 and $y % 2 == 0 and $z % 2 == 0) { print "All are even numbers\n"; } elsif ($x % 2 == 0 && $y % 2 == 0) { print "$z is the biggest odd number\n"; } elsif ($y % 2 == 0 && $z % 2 == 0) { print "$x is the biggest odd number\n"; } elsif ($x % 2 == 0 && $z % 2 == 0) { print "$y is the biggest odd number\n"; } elsif ($x % 2 == 0 && $y % 2 != 0 && $z % 2 != 0) { if ($y > $z) { print "$y is the greatest odd number\n"; } else { print "$z is the biggest odd number\n"; } } elsif ($y % 2 == 0 && $x % 2 != 0 && $z % 2 != 0) { if ($x > $z) { print "$x is the greatest odd number\n"; } else { print "$z is the biggest odd number\n"; } } elsif ($z % 2 == 0 && $x % 2 != 0 && $z % 2 != 0) { if ($x > $y) { print "$x is the greatest odd number\n"; } else { print "$y is the biggest odd number\n"; } } else { if ($x > $y && $x > $z) { print "$x is the biggest odd number\n"; } elsif ($y > $z) { print "$y is the biggest odd number\n"; } else { print "$z is the biggest odd number\n"; } }

    Update: As pointed out by Choroba, I did a typo at line 26. Sorry for the typo. I've fixed it below.

    use strict; use warnings; my ($x, $y, $z) = (-11,-13,-17); if ($x % 2 == 0 and $y % 2 == 0 and $z % 2 == 0) { print "All are even numbers\n"; } elsif ($x % 2 == 0 && $y % 2 == 0) { print "$z is the biggest odd number\n"; } elsif ($y % 2 == 0 && $z % 2 == 0) { print "$x is the biggest odd number\n"; } elsif ($x % 2 == 0 && $z % 2 == 0) { print "$y is the biggest odd number\n"; } elsif ($x % 2 == 0 && $y % 2 != 0 && $z % 2 != 0) { if ($y > $z) { print "$y is the greatest odd number\n"; } else { print "$z is the biggest odd number\n"; } } elsif ($y % 2 == 0 && $x % 2 != 0 && $z % 2 != 0) { if ($x > $z) { print "$x is the greatest odd number\n"; } else { print "$z is the biggest odd number\n"; } } elsif ($z % 2 == 0 && $x % 2 != 0 && $z % 2 != 0) { if ($x > $z) { print "$x is the greatest odd number\n"; } else { print "$z is the biggest odd number\n"; } } else { if ($x > $y && $x > $z) { print "$x is the biggest odd number\n"; } elsif ($y > $z) { print "$y is the biggest odd number\n"; } else { print "$z is the biggest odd number\n"; } }

    While this works fine for all the test cases I tried, but I will be thankful if you could please let me know a better way of writing this. Point to note is, it should only use if/else/elsif loops and / or comparison operators.


Empty STDIN does not exit while loop
6 direct replies — Read more / Contribute
by Anonymous Monk
on May 20, 2017 at 03:31

    Ok, it's not a huge issue and has an easy workaround, but I'm curious as to why this does not work. I want to break out of the while loop if an empty string is entered to STDIN. Am I missing something obvious due to sleep deprivation or lack of caffeine?

    print " > "; while (chomp( my $line = <STDIN>)){ print " [$line]\n"; } print " > ";

    I'm not looking for a solution, I already have that. Just wondering why this doesn't work

HMAC_MD5 with Net::SSH::Perl
1 direct reply — Read more / Contribute
by UpTide
on May 19, 2017 at 16:04

    On the main CPAN page http://search.cpan.org/~schwigon/Net-SSH-Perl/lib/Net/SSH/Perl.pm it says, and I quote "The deprecated hmac-sha1 or hmac-md5 algorithms are available but not enabled by default."
    Then It ends there. It does not say anything about how to enable sha1 or md5... I've been at this for hours trying to get it to use md5. I cannot use another SSH mac.
    The only thing I've found was Net::SSH::Perl::Mac, which just says it is implemented with Digest::HMAC_MD5, which just says 'This module provide HMAC-MD5 hashing.'
    I'm completely lost and very frustrated.


    EDIT: I found it finally. Example is as follows Net::SSH::Perl->new($serverHostname, debug => 1, options => ['MACs +hmac-sha1']) I was able to find this via the github page https://github.com/lkinley/Net-SSH-Perl found as the vague language "One can enable via the "MACs" option. For example: 'MACs +hmac-sha1'".
    I pray for those that have to also walk this path. Good luck.

'do' command is dead? Surely not?
1 direct reply — Read more / Contribute
by ACI
on May 19, 2017 at 09:57

    Hi there. This is infuriating me and I can't comprehend why it's happening. Best guess is that I've mangled the perl folder somehow. But re-installing the OS (Ubuntu Linux) doesn't help. Have I disabled something? Can I debug somehow? How can I find out why this is suddenly not working? Any assistance deeply appreciated!

    File: 'test.pl'

    print "Hello world!\n"; do 'test-response.pl'; print "Did that work?";
    File: 'test-response.pl' (same directory as 'test.pl')
    print "Hello Andy!\n";
    Output:
    Hello world! Did that work?

    Couldn't be simpler, surely? Done this a million times and never had a problem (unless my code has errors, which is not the problem here).

    Thanks in advance!

    Andy

printing every 2nd entry in a list backwards
7 direct replies — Read more / Contribute
by Anonymous Monk
on May 19, 2017 at 07:52

    Hallowed monks,

    lowly Perl apprentice here with a question regarding list traversal.

    I chanced upon this code written in a lesser language:

    import sys f = open(sys.argv[1], "r") for line in f: for n in line.split()[::-2]: sys.stdout.write(n + " ") print("");

    Being ever curious I wondered how this could be done in Perl.

    Trying to wield what little power of Perl I possess, I came up with the following:

    use strict; use warnings "all"; open(my $fh, "<", "$ARGV[0]") or die; while (<$fh>) { chomp; my $i = 0; map {print $i++ & 1 ? "" : "$_ "} reverse split(/ /); print "\n"; } close $fh;

    The task seems simple at a glance:

    Given a line of numbers from a file, print every 2nd number starting from the back.

    Example:

    1 22 3 -4 ==> -4 22

    Are the esteemed monks able to enlighten me with a solution that is better and more performant?

    Faithfully yours,

    Perl apprentice

search.cpan visibility vs meta
3 direct replies — Read more / Contribute
by stevieb
on May 18, 2017 at 20:03
Reading RW2 (Panasonic) raw digital image format
5 direct replies — Read more / Contribute
by BrowserUk
on May 18, 2017 at 17:14

    The title says it all really. I'm looking for a module (or format information) to allow me to read Panasonic RW2 format RAW DSC image files (and export them as 48-bpp tiff format.)

    Thanks.


    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". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit
Think Perl 6 (new book)
5 direct replies — Read more / Contribute
by pvaldes
on May 18, 2017 at 14:18
    It seems that this O' Reilly book is in pre-printing phase and estimate release of the paper version is "May 2017". (The Ebook version was released in January) Have you taken a look to the book yet? Is worth it? Any critic or opinion welcomed.
Request for help in method naming
4 direct replies — Read more / Contribute
by stevieb
on May 18, 2017 at 14:12

    Theoretical question bound for opinionated answers alert...

    So in my GPS distribution, I've decided that I want to make certain items toggle-able. First, some informational aspects are measured in metres-per-second by default. These include altitude, current speed and current lateral movement. I am trying to design a mechanism whereby someone presses a button (calls a method) it toggles between metric (metres) and imperial (feet). The other toggle I am creating is turning a signed decimal (latitude and longitude) into it's (for lack of better terms that I don't know yet) "English" counterpart. An example of this would be converting -114.1234567 into 114.1234567W (note the 'W' at the end).

    At first, I was only converting these internally, and only if a param was sent into the object instantiation method (new()). I have decided I want to make this functionality available at runtime.

    Now, at first, when the code was only being init in new, it was simple. I had a _metric() and _signed() methods. They make sense internally to me and to the software. The program run will either be metric or not, and signed or not.

    To expose these two options as methods, I can't think of a good name for them that encompasses what they do. For the 'metric' conversion, I plan on declaring like this: sub_name($decimal);. I could just work on the innards of the object where the relevant pieces are stored, but it would be handy for testing and other instances to be able to throw in any numbers, not just what we've collected. The declaration for the conversion of signed to "English" will be something like: sub_name($lat, $lon);.

    Internally, there's already a check to see whether something is metric or signed or not, so the toggle piece already works. It's the names I'm looking for. I don't want something crude like convert_lat_lon(). The name metric() *could* stand, but when going from metric to imperial, I kind of just don't like it.

    Looking for any and all suggestions for the naming conventions of these two simple methods :)

    Because it's Perlmonks, here's some untested code that I started to write before I came here for some insight...

    sub metric { my ($self, $stat, $metric) = @_; if ((! defined $metric || ! $metric) && ! $self->_is_metric) { $stat = $stat * 3.28084; $self->{tpv}{$_} = substr($stat, 0, index($stat, '.') +1 +3); $self->_is_metric(1); } }

    At the time, I was considering using the metric name and throwing in an extra param, but I really don't like that.

New Meditations
How easy it is to make mistakes
4 direct replies — Read more / Contribute
by tobyink
on May 23, 2017 at 06:54

    So, I don't use CGI a lot much these days. But I recently wrote a little joke module and decided to make a little HTML form interface for it. Plack would be a sensible way of doing the interface, but this was just a silly little thing and I was feeling lazy, so I coded it up using CGI instead. I'd used Moo rather than Moose so speed shouldn't have been an issue.

    I uploaded it to the server and visited the page and it just showed me my Perl source code. Hmmm…

    Create .htacccess and add the following lines:

    Options +ExecCGI AddHandler cgi-script .cgi .pl

    Visit the page again… still showing the source code. Hmmm…

    Scratch head for five minutes. Open up httpd.conf, add:

    <Directory /path/to/my/vhost/> AllowOverride all </Directory>

    Visit the page again… still showing the source code. Hmmm…

    SSH into the server and run ./myscript.cgi from the command line.

    "uniq" is not exported by the List::Util module

    Okay, I must have an old version of it. Upgrade.

    Visit the page again… still showing the source code. Hmmm…

    Stop using List::Util entirely.

    sub uniq { my %seen; grep !$seen{$_}++, @_; }

    Visit the page again… still showing the source code. Hmmm…

    Scratch head for ten minutes. Rename to ./myscript.pl.

    Visit the page again… still showing the source code. Hmmm…

    Create the following file and mark it as executable:

    #!/bin/sh echo "Content-Type: text/plain"; echo ""; echo "Hello world";

    Visit my new page… also showing the source code. Hmmm…

    Scratch head some more.

    Consider buying anti-headlice shampoo.

    Finally… FINALLY… after about TWO HOURS I figure out the problem! It was…

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the voices are still...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (11)
As of 2017-05-24 16:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?