Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
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
array search on mysql result
4 direct replies — Read more / Contribute
by rups
on Sep 20, 2014 at 06:07

    I have a database which contains mobile numbers. How do I write perl script which get all of numbers into array & check new number already exist or not in that array?

    Create Table:** CREATE TABLE consumeruser ( ConsumerId int(10) NOT NULL AUTO_INCREMENT, ConsumerName varchar(45) DEFAULT NULL, ConsumerMobNo varchar(10) DEFAULT NULL, PRIMARY KEY (ConsumerId) ) ENGINE=InnoDB AUTO_INCREMENT=4494 DEFAULT CHARSET=latin1
    #!/usr/bin/perl -w use strict; use DBI; use warnings qw(all); use Getopt::Long; use Pod::Usage; use Text::CSV_XS; my $username = 'root'; # set your MySQL username my $password = 'xxxx'; # set your MySQL password my $database = 'app'; # set your MySQL database name my $server = 'localhost'; # set your server hostname (probabl +y localhost) my $dbh = DBI->connect("DBI:mysql:$database;host=$server", $u +sername, $password) || die "Could not connect to database: $DBI::errstr"; my $CustomerMobileNumber = 9999999; my @MobileNumbers; my $mobileNumberQuery = "select ConsumerMobNo from consumerus +er"; my $sth = $dbh->prepare($mobileNumberQuery); $sth->execute(); while(my @row = $sth->fetchrow_array()){ push @MobileNumbers, $row; if (present){ 9999999 found in array; } else{ 9999999 not found in array; } }
call_pv on CORE:: sub from XS
1 direct reply — Read more / Contribute
by OlegG
on Sep 20, 2014 at 02:36
    I am trying to do this
    call_pv("CORE::socketpair", G_SCALAR);
    And this works excellent on perl 5.20, but fails on perl 5.14 with message "Undefined subroutine &CORE::socketpair called".

    So, is there a way to make such a thing on older perl?
flip flop operator and if statement
3 direct replies — Read more / Contribute
by iThunder
on Sep 19, 2014 at 22:32

    Hello,

    Can someone please help that how value of p is coming out as 1 through 17.

    My understanding is p should be equal to $. (line number) and it should be printed from 3 to 19 rather 1 to 17.

    Thanks in advance!

    #!/usr/bin/perl ##use warnings; while(<>){ if($p=3..1) { print "p is $p\n"; } }

    When i run the same code on a file that has fifteen lines, it returns following

    p is 1

    p is 2

    p is 3

    p is 4

    p is 5

    p is 6

    p is 7

    p is 8

    p is 9

    p is 10

    p is 11

    p is 12

    p is 13

    p is 14

    p is 15

    p is 16

    p is 17

    Also what is difference when i have $p=3..1 vs ($p=3)..1 in if statement above

How to convert negative and positive floating points to binary and vice versa
2 direct replies — Read more / Contribute
by thanos1983
on Sep 19, 2014 at 22:32

    Hello Monks,

    I have managed to convert positive and negative decimal numbers to binary and vice versa.

    Final Update (floating points (+/-) to binary and vice versa for 32 bit and 64 bit accuracy), the code is at the end in case of future reference.

    Sample of code:

    I am trying to figure out how to convert positive and negative floating points. For example if I had a number -0.000008 how could I convert the number in binary and back to floating.

    I have read on the pack documentation that: F A Perl internal floating-point value (NV) in native format

    When I am modifying on my code the "c" to "F" I get the following error:

    So I assume I am going terribly wrong some where. What I am missing?

    Final Update:

    Thanks to everyone's replies I manage to get this fixed, and understand that floating point 8 bits value is extremely small and not even worth existing.

    In IEEE 754-2008 the 16-bit base 2 format is officially referred to as binary16. It is intended for storage (of many floating-point values where higher precision need not be stored), not for performing arithmetic computations. Half-precision floating-point format

    So in conclusion I decided to use 32 bit floating points for positive and negative values.

    Sample of code provided bellow:

    In case the user wants to round up the values he can use functions like sprintf() or printf() etc.

    For example: printf("%.8f", $possitive_d); # prints 3.98765421 or my $rounded = sprintf("%.8f", $negative_d); # prints: -3.98765421 etc.

    For floating points 64 bits change the template from f to F and size from 32 to 64 an.

    Sample of working code provided under:

    Thanks again for everyone's assistance to help me understand the process and provided me so much material to read on.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
School assignment
3 direct replies — Read more / Contribute
by mb92
on Sep 19, 2014 at 20:07
    Hey,

    I need to write a perl script that searches for a file from standard input and then opens the file along with line numbers to the left of every line.. Been sitting for hours but haven't got it to work. Tips?

    I managed to get it work with the help of: system("find -type f -iname '$filename' -print -exec cat -n {} +"); but that is not pure perl code.
Faster indexing an array
5 direct replies — Read more / Contribute
by wollmers
on Sep 19, 2014 at 16:48

    Dear monks,

    as this part takes ~15% of an fast alignment algorithm, it would be nice to have a faster way.

    my $Y = [ qw( a b a) ]; my $YPos; my $index; for ( $index = 0 ; $index <= $#$Y ; $index++ ) { push ( @{ $YPos->{$Y->[$index]} }, $index ); } # now $YPos should contain my $result = { a => [ 0, 2 ], b => [ 1 ], };

    TIA (Thanks In Advance)

    Helmut Wollmersdorfer

Remove duplicate key value pair in hash
5 direct replies — Read more / Contribute
by reaper9187
on Sep 19, 2014 at 15:05
    Hello monks, I need some help eliminating duplicate key value pairs in a hash. The most simple reproducible example is as follows:
    my %hash = ('one' => '1', 'two' => '2','1'=>'one');
    As you can see, I have two entries with same key-value pairs. Is it possible to remove all such possible combinations ??

    Update

    Hello monks, I need some help eliminating duplicate key value pairs in a hash. The most simple reproducible example is as follows:
    my %hash = ('aaa' => 'bbb', 'bbb' => 'aaa','ccc'=>'bbb','bbc'=>'ccc',' +abc'=>'123');
    As you can see, I have two entries with same key-value pairs. Is it possible to remove all such possible combinations ??
sql datetime value with today
4 direct replies — Read more / Contribute
by fionbarr
on Sep 19, 2014 at 15:02
    I am retrieving a sql datetime value that looks like:
    2012-01-31 00:00:00.000
    I am comparing with 'today' with regex's and I'm sure there is a much better way.
sprintf to hash
3 direct replies — Read more / Contribute
by teamassociated
on Sep 19, 2014 at 14:45
    Been trying to figure out why this code is not storing my sprintf output. Please help! Thx!!!
    use strict; use warnings; $lsmapFH = new FileHandle "/usr/ios/cli/ioscli lsmap -vadapter $option +s[4] |" or die $!; while (<$lsmapFH>) { if (/no virtual device found/i) { warn "\n\nThis vhost $options[4] has no clients using me, tr +y again with a correct vhost #!\n\n"; die $!; } chomp; $vh = (split)[0] if /^vhost/i; $clid = (split)[2] if /^vhost/i; $cltid = sprintf "%d", $clid; } $lsmapHash{$vh} = { 'clientID' => $cltid, }; } use Data::Dumper; print Dumper \%lsmapHash; $VAR1 = { 'vhost10' => { 'clientID' => '0' } works fine in CLI: # perl -le '$clid=0x0000000a; $cltid= sprintf "%d", $clid; print $clti +d;' 10 __SAMPLE_OUTPUT__ from lsmap SVSA Physloc Client Pa +rtition ID --------------- -------------------------------------------- --------- +--------- vhost10 U9117.MMD.218A8B7-V1-C2 0x0000000b VTD vtscsi25 Status Available LUN 0x8100000000000000 Backing device l02duapXXXX_r1.58ff94782d7c325acc49a8dd9c04bb1e Physloc Mirrored N/A VTD vtscsi26 Status Available LUN 0x8200000000000000 Backing device l02duapofdev_b1.0ee86903d4951ce38a9b691b322ff1d2 Physloc Mirrored N/A
Stuttering Children
1 direct reply — Read more / Contribute
by cmv
on Sep 19, 2014 at 14:21
    Hi Monks-

    Consider the attached script.

    I believe I should get 2 lines of output from each of the 2 children. Running this under perl, v5.8.8 built for darwin works fine. However, when I run it under perl, v5.8.8 built on solaris, strange things happen:

    TRY1: CHILD1: My pid = 21727 CHILD1: 1: THIS IS YOUR PARENT SPEAKING... CHILD1: 2: THIS IS YOUR PARENT SPEAKING... CHILD1: 1: THIS IS YOUR PARENT SPEAKING... CHILD1: 2: THIS IS YOUR PARENT SPEAKING... CHILD2: My pid = 21728 CHILD2: 1: THIS IS YOUR PARENT SPEAKING... CHILD2: 2: THIS IS YOUR PARENT SPEAKING... TRY2: CHILD1: My pid = 22218 CHILD1: 1: THIS IS YOUR PARENT SPEAKING... CHILD1: 2: THIS IS YOUR PARENT SPEAKING... CHILD2: My pid = 22219 CHILD2: 1: THIS IS YOUR PARENT SPEAKING... CHILD2: 2: THIS IS YOUR PARENT SPEAKING... CHILD2: 1: THIS IS YOUR PARENT SPEAKING... CHILD2: 2: THIS IS YOUR PARENT SPEAKING...
    First, why do I sometimes get 4 lines from one “bad” kid??

    Second, why does the “bad” kid sometimes change?

    Any pointers very much appreciated!

    Thanks

    -Craig

    #!/opt/exp/bin/perl use strict; use warnings; foreach my $i (1..2) { _spawn($i); } sub _spawn { my $id = shift || die "Missing id\n"; my $pid = fork(); defined $pid or die "bad open (pipe/fork): $!\n"; # Have parent/child run their respective code... if ( $pid ) { # PARENT CODE... return; } else { # CHILD CODE... print "CHILD$id: My pid = $$\n" ; while(<DATA>) { print "CHILD$id: $_" }; exit } } __DATA__ 1: THIS IS YOUR PARENT SPEAKING... 2: THIS IS YOUR PARENT SPEAKING...

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 exploiting the Monastery: (5)
    As of 2014-09-22 02:44 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (177 votes), past polls