Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
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
Test:: to match multiple lines against corresponding regular expressions
1 direct reply — Read more / Contribute
by Dallaylaen
on Jun 24, 2018 at 08:05

    Hello dear esteemed monks!

    I'm looking for a way to test multiple lines to match corresponding regular expressions:

    multi_like \@array, [ qr/regex1/, qr/regex2/, .... ], "Test name";

    Now obviously I can do it with a subtest and like statements, plus one test for length:

    subtest "All lines are like expected" => sub { like $array[0], qr/regex1/; like $array[1], qr/regex2/; .... is scalar @array, $nnn, "No extra lines"; };

    And I can do it with one go with Test::Deep, although I haven't yet found out how to make it report the full difference and not just the first mismatch.

    But maybe there's a module that does exactly that (matches a list of strings against a list of regexps)?

    Thanks!

Print contents of text file line by line
6 direct replies — Read more / Contribute
by TonyNY
on Jun 23, 2018 at 14:55

    Hi,

    How can I read the contents of a text file and print each line one by one?

    I've tried the following but it only prints "something here" a whole bunch of times.

    $filename='result.txt'; open( my $fh, '<', $filename ) or die "Can't open $filename: $!"; while ( my $line = <$fh> ) { print "something here $line[0]"; print "something here $line[1]"; print "something here $line[2]"; } close $fh;

    Thanks

help with modules@perl.org
1 direct reply — Read more / Contribute
by dk
on Jun 23, 2018 at 13:25
    Hello all,

    I'm trying to take over an abandoned CPAN module, and am trying to discuss this at modules@perl.org as I did long time ago a couple of times. However there's something wrong with subscription - my subscription mails, and also my mails with questions there doesn't seem to go through - no bounce, nothing.

    Is there a person that could be able to help me with the mailing list and/or, module takeover?

    /dk

Strange and Unusual
1 direct reply — Read more / Contribute
by itsmeiggit
on Jun 23, 2018 at 12:24
    Script that had been running fine stopped writing to a folder. Then it stopped retaining values to cross reference to not duplicate. Replaced it. Worked fine in another folder. Checked folder. Was chmod 777. What the? Anybody anywhere ever get faced with this ridiculous set of events? Any trouble shooting potential tips? Thanks in advance.
Unable to extract value from hash table
2 direct replies — Read more / Contribute
by NsshB
on Jun 23, 2018 at 10:40

    I am parsing a log file and trying to validate a value based on conditions within the log file using one of several hash tables. I am not getting any output and do not understand why.

    Input file:
    CMD, 2018/06/22, 14:21:44, ON, "GOBBLDY GOOK", PRI, "MORE GOBBLDY GOOK",
    STAT, 2018/06,22, 14:21:44, A, ZELDA, "GOBBLDY GOOK",
    STAT, 2018/06,22, 14:21:44, A, YAKOV, "GOBBLDY GOOK",
    STAT, 2018/06,22, 14:21:44, U, XAVIER, "GOBBLDY GOOK",
    STAT, 2018/06,22, 14:21:44, U, WALTER, "GOBBLDY GOOK",
    STAT, 2018/06,22, 14:21:44, U, VICKY, "GOBBLDY GOOK",
    CMD, 2018/06/22, 14:21:44, ON, "GOBBLDY GOOK", BU, "MORE GOBBLDY GOOK",
    STAT, 2018/06,22, 14:21:44, A, ALISTAIR, "GOBBLDY GOOK",
    STAT, 2018/06,22, 14:21:44, A, BARB, "GOBBLDY GOOK",
    STAT, 2018/06,22, 14:21:44, U, CONNOR, "GOBBLDY GOOK",
    STAT, 2018/06,22, 14:21:44, U, DENISE, "GOBBLDY GOOK",
    STAT, 2018/06,22, 14:21:44, U, ERIN, "GOBBLDY GOOK",
    CMD, 2018/06/22, 14:21:44, OFF, "GOBBLDY GOOK", PRI, "MORE GOBBLDY GOOK",
    STAT, 2018/06,22, 14:21:44, U, ZELDA, "GOBBLDY GOOK",
    STAT, 2018/06,22, 14:21:44, U, YAKOV, "GOBBLDY GOOK",
    STAT, 2018/06,22, 14:21:44, A, XAVIER, "GOBBLDY GOOK",
    STAT, 2018/06,22, 14:21:44, A, WALTER, "GOBBLDY GOOK",
    STAT, 2018/06,22, 14:21:44, A, VICKY, "GOBBLDY GOOK",
    CMD, 2018/06/22, 14:21:44, OFF, "GOBBLDY GOOK", BU, "MORE GOBBLDY GOOK",
    STAT, 2018/06,22, 14:21:44, U, ALISTAIR, "GOBBLDY GOOK",
    STAT, 2018/06,22, 14:21:44, U, BARB, "GOBBLDY GOOK",
    STAT, 2018/06,22, 14:21:44, A, CONNOR, "GOBBLDY GOOK",
    STAT, 2018/06,22, 14:21:44, A, DENISE, "GOBBLDY GOOK",
    STAT, 2018/06,22, 14:21:44, A, ERIN, "GOBBLDY GOOK",

    #! /usr/bin/perl my %dayTable0 = ( "ZELDA" => "U", "YAKOV" => "U", "XAVIER" => "A", "WALTER" => "A", "VICKY" => "A", ); my %dayTable1 = ( "ZELDA" => "A", "YAKOV" => "A", "XAVIER" => "U", "WALTER" => "U", "VICKY" => "U", ); my %nightTable0 = ( "ALISTAIR" => "U", "BARB" => "U", "CONNOR" => "A", "DENISE" => "A", "ERIN" => "A", ); my %nightTable1 = ( "ALISTAIR" => "A", "BARB" => "A", "CONNOR" => "U", "DENISE" => "U", "ERIN" => "U", ); open (FILE, my $file) or die "Can't open $file $!\n"; chomp (my @lines = <FILE>); foreach my $line (@lines) { my @line = split (/,/,$line); if ($line[0] =~ /CMD/) { my $state = $line[3]; my $unit = $line[5]; if ($state =~ /OFF/ && $unit =~ /PRI/) { %table = %dayTable0; } elsif ($state =~ /ON/ && $unit =~ /PRI/) { %table = %dayTable1; } elsif ($state =~ /OFF/ && $unit =~ /BU/) { %table = %nightTable0; } elsif ($state =~ /ON/ && $unit =~ /BU/) { %table = %nightTable1; } else { print "UNKNOWN STATE!! GO FIX SOMETHING!!!"; } } if ($line[0] =~ /STAT/) { my $name = $line[5]; my $status = $line[4]; my $value = $table{$name}; <p> Have tried several variants of above line. $table{ZELDA} gives me +the expected static response<\p> print "LOOKUP STATUS: $value\n"; } }
Overloading Weirdness
3 direct replies — Read more / Contribute
by pudge
on Jun 23, 2018 at 01:40
    Long story short: we want to mark strings so that later we can do something with them, even if they get embedded in other strings. So we figured, hey, let's try overloading. It is pretty neat. I can do something like:
    my $str = str::new('<encode this later>'); my $html = "<html>$str</html>"; print $html; # <html><encode this later></html> print $html->encode; # <html>&lt;encode this later&gt;</html>
    It does this by overloading the concatenation operator to make a new object array with the plain string "<html>", the object wrapping "<encode this later>", and the plain string "</html>". It can nest these arbitrarily. On encode, it will leave the plain strings, but encode the object strings. But if you stringify the object, it just spits it all out as plain strings. This works well, except that in some cases, it stringifies for no apparent reason. The script below shows the behavior, which I've duplicated in 5.10 through 5.22.
    #!/usr/bin/perl use strict; use warnings; use 5.010; use Data::Dumper; $Data::Dumper::Sortkeys=1; my $str1 = str::new('foo'); my $str2 = str::new('bar'); my $good1 = "$str1 $str2"; my $good2; $good2 = $good1; my($good3, $good4); $good3 = "$str1 a"; $good4 = "a $str1"; my($bad1, $bad2, $bad3); $bad1 = "a $str1 a"; $bad2 = "$str1 $str2"; $bad3 = "a $str1 a $str2 a"; say Dumper { GOOD => [$good1, $good2, $good3], BAD => [$bad1, $bad2, $ +bad3] }; $bad1 = ''."a $str1 a"; $bad2 = ''."$str1 $str2"; $bad3 = ''."a $str1 a $str2 a"; say Dumper { BAD_GOOD => [$bad1, $bad2, $bad3] }; package str; use Data::Dumper; $Data::Dumper::Sortkeys=1; use strict; use warnings; use 5.010; use Scalar::Util 'reftype'; use overload ( '""' => \&stringify, '.' => \&concat, ); sub new { my($value) = @_; bless((ref $value ? $value : \$value), __PACKAGE__); } sub stringify { my($str) = @_; #say Dumper { stringify => \@_ }; if (reftype($str) eq 'ARRAY') { return join '', @$str; } else { $$str; } } sub concat { my($s1, $s2, $inverted) = @_; #say Dumper { concat => \@_ }; return new( $inverted ? [$s2, $s1] : [$s1, $s2] ); } 1;
    I want all of these to be dumped as objects, not strings. But the "BAD" examples are all stringified. All of the "BAD" examples are when I'm assigning a string object I am concatenating at the moment to a variable previously declared. If I declare at the same time, or concatenate the strings previously, or add in an extra concatenation (beyond the interpolated string concat), then it works fine. This is nuts. The result of the script:
    $VAR1 = { 'BAD' => [ 'a foo a', 'foo bar', 'a foo a bar a' ], 'GOOD' => [ bless( [ bless( [ bless( do{\(my $o = 'foo')}, ' +str' ), ' ' ], 'str' ), bless( do{\(my $o = 'bar')}, 'str' ) ], 'str' ), $VAR1->{'GOOD'}[0], bless( [ $VAR1->{'GOOD'}[0][0][0], ' a' ], 'str' ) ] }; $VAR1 = { 'BAD_GOOD' => [ bless( [ '', bless( [ bless( [ 'a ', bless( do{\(my $o + = 'foo')}, 'str' ) ], 'str' ), ' a' ], 'str' ) ], 'str' ), bless( [ '', bless( [ bless( [ $VAR1->{'BAD_GOOD +'}[0][1][0][1], ' ' ], 'str' ), bless( do{\(my $o = 'bar') +}, 'str' ) ], 'str' ) ], 'str' ), bless( [ '', bless( [ bless( [ bless( [ bless( [ + 'a ', + $VAR1->{'BAD_GOOD'}[0][1][0][1] ] +, 'str' ), ' a ' ], 'str' ) +, $VAR1->{'BAD_GOOD +'}[1][1][1] ], 'str' ), ' a' ], 'str' ) ], 'str' ) ] };
    The behavior makes no sense to me. I'd like to understand why it works this way, and I'd like to find a workaround.
finding sequence
4 direct replies — Read more / Contribute
by yueli711
on Jun 22, 2018 at 14:05

    Hello, I want to find the sequences in tmp01 which are containing in tmp02, then print all of them out. Tab seperated tmp02 file. Thanks in advance!

    tmp01 ATCCCACCGCTGCCACCA ACCCTGCTCGCTGCGCCA TCCCCGGCACCTCCACCA TCCCCGGCATCTCCACCA ATCCTGCCGACTACGCCA TCGATTCCCGGCCCATGCACCA TCGATTCCCGGCCAACGCACCA GTCCCACCAGAGTCGCCA ACCCCACTCCTGGTACCA GTCCCTTCGTGGTCGCCA tmp02 AACCCCATCCCACCGCTGCCACCA 1 AACCCCATCCTCGTCGCC 1 AACCCCATGAAATAAGAG 2 AACCCCATGATCAGGACAAG 1 AACCCCATTAAAAAATGG 1 AACTGGATTCTCTGAAATCCCACCGCTGCCACCA 1 AACTGGATTGTCTGTTTGT 1 AACTGGCAAGTTCAGGCATG 1 AACTGGCACACACAACC 1 AACTGGCACACACAACCT 1
    open(IN1,"tmp01") || die "Cannot open this file"; @lines1 = <IN1>; open(IN2,"tmp02") || die "Cannot open this file"; @lines2 = <IN2>; open(OUT,">tmp03") || die "Cannot open this file"; for $item1(@lines1){ chomp $item1; #print OUT $item1,"\t"; @tmp1=split(/\t+/, $item1); for $item2(@lines2){ chomp $item2; @tmp2=split(/\t+/, $item2); if ($tmp1[0] =~m/ *$tmp2[0]*){ print OUT $item1,"\t",$item2; #last; } $i++ } print OUT "\n"; } close(IN1); close(IN2); close(OUT);
Using 'no warnings' to disable own debug warn(ing)s?
1 direct reply — Read more / Contribute
by LanX
on Jun 22, 2018 at 13:13
    According to warnings and perldiag it's possible to disable special warning classes in a lexical scope. (like the uninitialized one in the example)

    Is it also possible to use this mechanism to disable my own debug messages for a scope?

    (I'm aware about the possibility to manipulate $SIG{"__WARN__"} handler, but trying not to reinvent the wheelฒ.)

    use strict; use warnings; my $x; { no warnings; # no warnings 'uninitialized'; print "$x"; warn "inside"; } print "$x"; warn "outside";
    inside at d:/Users/lanx/pm/no_warn.pl line 9. Use of uninitialized value $x in string at d:/Users/lanx/pm/no_warn.pl + line 11. outside at d:/Users/lanx/pm/no_warn.pl line 12.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

    ฒ) and overusing Pad::Walker

PAR::Packer Fail install Perlbrew
1 direct reply — Read more / Contribute
by Anonymous Monk
on Jun 22, 2018 at 12:24

    I am on macOS (HighSierra) using Perlbrew. I have problems to install PAR::Packer, as it fails with this error:

    --> Working on PAR::Packer Fetching http://www.cpan.org/authors/id/R/RS/RSCHUPP/PAR-Packer-1.045. +tar.gz ... OK ==> Found dependencies: DynaLoader skipping N/NW/NWCLARK/perl-5.8.6.tar.gz ! Installing the dependencies failed: Module 'DynaLoader' is not insta +lled ! Bailing out the installation for PAR-Packer-1.045.

    I have on the same machine several Perl installation. Never had a problem to install this module. However, I noticed that if I use

    perlbrew available

    I get a rather short list of cperl. This was not the case until a couple of months ago, where I got a list of perl. I have PAR::Packer installed with perl-5.24.4, however on both cperl-5.27.2 and cperl-5.26.2 it fails with me message above. Any idea?

not well-formed (invalid token) while using SOAP Lite Module
No replies — Read more | Post response
by Perl_Jay
on Jun 22, 2018 at 11:11

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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (8)
    As of 2018-06-24 17:08 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?



      Results (126 votes). Check out past polls.

      Notices?