Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
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
rough approximation to pattern matching using local
3 direct replies — Read more / Contribute
by gregory-nisbet
on Jan 25, 2015 at 00:42
    I am, as an exercise, trying to write a function that mimics pattern matching in langauges like Haskell and OCaml in Perl. The following code runs (I'm using Perl 5.18) and produces the following result.
    $ perl pattern_matching.pl Name "main::bob" used only once: possible typo at pattern_matching.pl +line 50. Use of uninitialized value $bob in print at pattern_matching.pl line 5 +0.
    I think that's because the while loop
    while (my ($k, $v) = each %$locals) { local ${$k} = $v; }
    localizes each variable only within the body of the loop. I'm wondering what the right approach would be. Entire code below.
    use strict; use warnings; sub same_keys { my ($left, $right) = @_; return unless ref $left eq 'HASH' and ref $right eq 'HASH'; return unless keys %$left == keys %$right; for my $key (keys %$left) { exists $right->{$key} or return; } return 1; } sub match ($$$) { no strict 'refs'; my ($lhs, $rhs, $fun) = @_; my $locals = {}; # populate locals match_inner($lhs, $rhs, $locals); while (my ($k, $v) = each %$locals) { local ${$k} = $v; } return $fun->(); } sub match_inner { my ($lhs, $rhs, $locals) = @_; if (ref $lhs eq '') { $locals->{$lhs} = $rhs; } elsif (ref $lhs eq 'ARRAY') { if (@$lhs == @$rhs) { for my $i (0 .. $#$lhs) { match_inner($lhs->[$i], $rhs->[$i], $locals); } } } elsif (ref $lhs eq 'HASH') { if (same_keys($lhs,$rhs)) { for my $k (keys %$lhs) { match_inner($lhs->{$k}, $rhs->{$k}, $locals); } } } } match "bob", 45, sub { no strict; print $bob; };
count of USER UNIX groups
1 direct reply — Read more / Contribute
by Apronline
on Jan 24, 2015 at 21:57
    Hi, Can you please let me know how to find a count of USER UNIX groups in perl?
How to print a multi-level Hashes of Hashes without the use of a module
2 direct replies — Read more / Contribute
by thanos1983
on Jan 24, 2015 at 20:30

    Hello Monks,

    Although that this seems with a very common question I can not find solution to my problem. I have a hash that contains multiple levels and I am trying to extract the data of the final level.

    I want to use for loops to reach the final level and then store the data to one level hash.

    I have been reading online the perldoc Access and Printing of a HASH OF HASHES. Even I found a similar question Traverse an unknown multi-dimensional hash but it is not exactly what I want.

    On the sample of code that I have created I can see the path of the hash clearly by using Data::Dumper but I want to use foreach loops so I can reach the final stage of my complex hash and extract the data to a simple single level hash.

    Update: The %singleLevelHash = (); that I have defined I wan to contain the last level of data.

    Sample of desired output:

    %singleLevelHash = ( 'thirdLevelSampleKeyOne' => 'thirdLevelSampleValue', 'thirdLevelSampleKeyTwo' => 'thirdLevelSampleValue', 'thirdLevelSampleKeyThree' => 'thirdLevelSampleValue', );

    This is the reason that I wanted to use the foreach loop. Sample of my code provided under.

    Sample of code:

    Any suggestions are much appreciated.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
hash in perl
2 direct replies — Read more / Contribute
by nono
on Jan 24, 2015 at 14:37
    #! /usr/bin/perl use strict;use warnings; my %data; open(IN,"data.file"); while(<IN>){ chomp; my @line=split(/\t/,$_); $data{$line10}{$line3}=$line2; foreach my {$line10}(keys %data){ print"$data{$line10}\t$data{line3}\t$data{$line2}\n" }} I did that script but when I run it , I got this error message Missing $ on loop variable at mouse5.pl line 16. please could anyone help me in that issue
help with splitting and printing the array
1 direct reply — Read more / Contribute
by perlnewbie012215
on Jan 24, 2015 at 14:36

    Hi All Monks,

    I am pretty new to perl and I wrote a small perl script to ease my pain, did not work.

    My input looks like this with over 20 line in the text file.

    18:06:04.616 79952.57692 <2> logparams: -S server1.prod.com -D server2.prod.com -C server3.prod.com -X -s 1421467204 -e 1421467204 -R E:\Program Files\725635\logs\log.tmp.chg -K -t 13 -M -k -L E:\Program Files\725635\logs\infod.tmp.log -W -f E:\Program Files\725635\logs\after.tmp

    17:49:34.216 58140.81564 <2> logparams: -S -l -a -k server1.prod.com -D client10.prod.com -C sql12.prod.com -X -s 1421467205 -e 1421467205 -R E:\Program Files\561234\logs\log.tmp.chg -K -t 13 -M -k -L E:\Program Files\561234\infod10.tmp.log -W -f E:\Program Files\561234\logs\after.tmp

    and my output should look like this:

    server1.prod.com,server2.prod.com,server3.prod.com,725635,infod.tmp.log

    server1.prod.com,client10.prod.com,sql12.prod.com,561234,infod10.tmp.log

    My script look like this, it worked only when the count is right as you can see the second line the count went way off, the fields count in my code is not accurate but you get the point. I need a different logic, can you guys help me out.. Thank you all

    open(FILE, $filename) or die "Could not read from $filename, program h +alting."; while(<FILE>) { # get rid of the pesky newline character chomp; # read the fields in the current record into an array @fields = split(/[\s\\]+/, $_); $datestring = localtime(); open logreport, ">>E:\\temp\\logs\\eligiblereport.txt"; print logreport "$date,$fields[5],$fields[7],$fields[9],$fields[24] +,$fields[43]\n"; } close logreport; close FILE;
Substitution backreference woes
5 direct replies — Read more / Contribute
by Anonymous Monk
on Jan 24, 2015 at 07:43

    I'm trying to work out how Perl substitutions work. We're told that backreference patterns can be used in the substitution by specifying \1, \2 (etc), but it doesn't seem to work.

    Snippet below. It tries to turn the string 'Hello sailor' into 'Goodbye sailor', but I get 'Goodbye \1' instead.

    (Obviously this isn't sensible way to convert one string to the other, but this is just an example.)

    #!/usr/bin/perl use strict; use diagnostics; use warnings; my $string = 'Hello sailor'; my $regex = 'Hello (.*)'; my $substitution = 'Goodbye \1'; $string =~ s/$regex/$substitution/; print "string is now $string \n"; # I wanted: string is now Good bye sailor # I got: string is now Good bye \1
Break sentence into two part
6 direct replies — Read more / Contribute
by Mjpaddy
on Jan 24, 2015 at 01:36

    Hello monks, Is there is any module or method which can make a sentence into two equal part or half or partially half

    Suppose this is a sentence as follow:

    Perl 5 is a highly capable, feature-rich programming language with over 27 years of development.

    which method will be used in order to get result like:

    $firsthalf: Perl 5 is a highly capable, feature-rich programming

    $sechalf : language with over 27 years of development.

    Thanks in advanced

Whenever perl meets namedpipe it gets blocked
3 direct replies — Read more / Contribute
by Anonymous Monk
on Jan 23, 2015 at 13:29

    I wrote a simple that prints only files recursively. But I have a problem.
    whenever It meets named pipe, it gets blocked at all!
    I thought it would be ok if I add just -p test,but that's too late because program is blocked when opening the named pipe!

    #!/usr/bin/perl -w use latest; use File::Spec; my $test=$ARGV[0]; my $ds='/'; sub bfs { my $path=$_[0]; push(my @queue,$path); my ($current,@output); while(@queue) { $path=shift(@queue); if($path=~/${ds}\.{1,2}$/){next} opendir($current,$path) or open($current,$path) or next; if(-d $current) { push @queue,map {File::Spec->catfile($path,$_)} readdir $c +urrent; closedir $current } elsif(-f $current) { say $path; close $current; push @output,$path; } } @output; } bfs($test);
Replicate a website GUI functionality
1 direct reply — Read more / Contribute
by doubledecker
on Jan 23, 2015 at 13:10

    Hi Monks

    I want to implement / replicate the functionality of the following website.

    https://xkpasswd.net/s/

    The core functionality is written using Perl. However, I am interested in replicating the GUI of this website. Any thoughts on how to implement and technologies involved.

Search through array of hashrefs and store matching
1 direct reply — Read more / Contribute
by sotona
on Jan 23, 2015 at 11:39
    Hi Monks,

    I have an array e.g.
    my @array = ( {x => 1, y => 2, z => 3}, {x => 4, y => 5, z => 6}, #etc );
    I can search through it:
    grep {$_->{x} == 4 } @array;
    but how do I store the entire hash whith matching key?

    UPD:Please delete this question. I was too tired to use my brain :-)
    my @newarray = grep {$_->{x} == 4 } @array;

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 lurking in the Monastery: (7)
    As of 2015-01-25 13:50 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My top resolution in 2015 is:

















      Results (182 votes), past polls