Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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
Populating a hash-ref from multiple arrays with slice?
4 direct replies — Read more / Contribute
by Anonymous Monk
on Apr 16, 2015 at 13:33
    Hi perl monks. I am stumped with this one.

    I have several arrays that I want to slice into a single hash ref. Any tips? I read a bit about creating a hash slice from 2 arrays, but can't figure out the syntax for creating a hash ref slice from multiple arrays.

    For example (excuse grammatical errors -- this is more pseudo code than anything):

    $hash_ref; @array1 = "some_unique_key1 some_unique_key2 some_unique_key3"; @array2 = "meta_data_1 metadata_2 metadat_3"; @array3 = "submitted_date1 submitted_date2 submitted_date3";

    The hash ref would have a structure like:

    $some_uniquekey1 --> 'Metadata' --> $meta_data1 = '1' --> 'Submitted Date' --> $subitted_date1 = '1' $some_uniquekey2 ... etc $some_uniquekey3 ... etc

    Basically the arrays value indices all correspond to each other -- I just want to merge them all into an accessible table.

    Any tips or slaps up the side of my head appreciated!

Changing a variable which is a value of a hash
5 direct replies — Read more / Contribute
by benedicth
on Apr 16, 2015 at 11:40
    Suppose there is a variable which is then declared a value of a hash. I then want to change $test to the content of $change but not change the hash reference. The result should be $test = 'change' while $hash{t} stays $test.
    $test = 'test'; %hash = ( t => $test); $change = 'changed'; $hash{t} = $change; #I just want to change $test to 'changed' not $hash{t} to $change
    I understand this is wrong but can't find an explanation for using variables as hash values. thanks for your help pros.
Net::FTP with Xlight FTP server
2 direct replies — Read more / Contribute
by perl_help26
on Apr 16, 2015 at 06:45

    Hello :)

    I am trying to send a file through ftp from my machine to a virtual Xlight FTP server. The files are indeed sent to the ftp server BUT they are empty. I want to know if this something to do with the FTP code I am using:

    my $ftp = Net::FTP->new($xlight_server_ip, Debug => 0) or log_error +("Cannot connect") ; $ftp->login($user,$pass) or log_error ("Cannot login ") ; $ftp->cwd($dest_dir) or log_error ("Cannot change working directory ") + ; $ftp->put($file_to_send);

    Any ideas??? Thanks

LCS algorithm
3 direct replies — Read more / Contribute
by porl
on Apr 16, 2015 at 06:24

    Hi! I am new to Perl and very stuck. I am trying to create a Perl implementation of the LCS algorithm
    I am required to do a comparison of two text files without the use of Text::Diff and I THINK(suggestions welcomed) this is how I should be go about it. I am having trouble converting the pseudo code functions found in the link. This is what I have so far but have no idea if I am on the right track.

    sub wikiLCSLength { #$file1 = $_[0]; #$file2 = $_[1]; #$file1 = "i b c d e f g h i"; #$file2 = "a b c d e f f f f"; @m = ("a", "b", "c", "d", "e"); @n = ("a", "b", "c", "e", "e"); $mLength = scalar @m; $nLength = scalar @n; #Initialize the multidimensional array for(my $i = 0; $i<= $mLength; $i++) { for(my $j = 0; $j<= $nLength; $j++) { $C[$i][$j] = 0; } } for($i = 0; $i <= $mLength; $i++) { $C[$i][0] = 0; } for($j = 0; $j <= $nLength; $j++) { $C[0][$j] = 0; } for($i=1; $i<$mLength; $i++) { for($j=1; $j<$nLength; $j++) { if($m[$i] eq $n[$j]) { $C[$i][$j] = $C[$i-1][$j-1] + 1; } else { $C[$i][$j] = max(($C[$i][$j-1]),($C[$i-1][$j])); } } } &wikiBacktrack(\@C, \@m, \@n, $mLength, $nLength); } sub wikiBacktrack { @C = @{$_[0]}; @m = @{$_[1]}; @n = @{$_[2]}; $mLength = $_[3]; $nLength = $_[4]; print("\n $n[5] \n"); #BACKTRACKIN BB if($mLength==0 || $nLength==0) { return (""); } elsif($m[$mLength] eq $n[$nLength]) { return &wikiBacktrack(@C, @m, @n, $mLength-1, $nLength-1) + $m +[$mLength]; } else { if($C[$mLength][$nLength-1] > $C[$mLength-1][$nLength]) { return &wikiBacktrack(@C, @m, @n, $mLength, $nLength-1); } else { return &wikiBacktrack(@C, @m, @n, $mLength-1, $nLength); } } }

    Any help would be greatly appreciated. Thanks in advance!

Matching text in a string
3 direct replies — Read more / Contribute
by htmanning
on Apr 16, 2015 at 05:08
    Monks, I hope you can tell what I'm trying to do here even though there is a flaw in my code. It does not work. I'm trying to insert a first name if it is not already in a text field.
    if ($text !=~ m/$firstname/ && $type eq "login") { $text = "<b>$firstname:</b> " . $text; }
    I'm not sure what I'm doing wrong, but it inserts the first name in every text field that has the type login.
shuffle in Parallel::ForkManager;
2 direct replies — Read more / Contribute
by plagent
on Apr 16, 2015 at 03:57
    use strict; use List::Util qw(shuffle); use Parallel::ForkManager; my $multi_cpu_num = 2; my @chars_1 = split '', 'ACTCTGCATGCATACGCATCAGCATCAT'; my @chars_2 = split '', 'TAGCTCATGTCGATGCATGCTCGTCTCG'; my $pm = Parallel::ForkManager->new( $multi_cpu_num ); PWM: for my $pwm (1..5) { $pm->start and next PWM; my @chars_1_temp = shuffle @chars_1; my @chars_2_temp = shuffle @chars_2; print "this is $pwm....\n"; print join '',@chars_1_temp,"\n"; print join '',@chars_2_temp,"\n"; $pm->finish; } $pm->wait_all_children;
    I tried to use the above code to generate 5 different paired sequences using 2 CPU. But the result was only one paired sequences generated, the other four were cloned paired without shuffle. They were identical each other. I expected those five paired sequences were different. Why? If I do not use Parallel::ForkManager module, then everything is OK.
Approximate logarithmic statistical module: new() params naming
1 direct reply — Read more / Contribute
by Dallaylaen
on Apr 16, 2015 at 02:37

    Suppose we have a module for approximate memory-efficient statistical analysis which stores data in a set of logarithmic bins. However, around zero, depending on the data, it may be suitable to switch to linear interpolation (as in "no measurement is absolutely precise, why use so many bins").

    For now, the proposed new() interface (has not been released to CPAN) is as follows:

    • base - base of logarithmic intervals, i.e. upper_bound/lower_bound;
    • precision - width of linear intervals around zero;
    • zero_thresh - optional threshold at which to switch to linear, normally it's calculated dynamically (we want to switch where the linear and logarithmic intervals are of the same width).

    I'm ok with my data model, but the parameter names seem a bit weird.

    I would like to rename them to relative_precision, absolute_precision, and linear_threshold respectively. Does that look clear enough?

    I was also thinking of absolute/relative error, but error is really variable and no more than half the precision. I think this could cause additional WTF.

    Are there any better ideas?

    The module in question is Statistics::Descriptive::LogScale. Here's the previous discussion.

Split very big string in half
7 direct replies — Read more / Contribute
by fpscolin
on Apr 15, 2015 at 14:55

    Hello Monks,
    I've encountered a problem while trying to split a string. This string is a Slurp of a large HTML file (~3million characters long, no images), which needs to be split into two smaller strings in order to be passed to a PDF converter. At the moment, this is what I have:

    if (length($html[$i]) > 1000000) ## $html[$i] holds the current string + of html { my @bigHTML = ### Split here #########; for (my $z=0; $z <= $#bigHTML; $z++) { open (my $file, '>', "giant_${i}_$z.htm") or die $!; print $file $bigHTML[$z]; close $file; push(@lists, "giant_${i}_$z.htm"); } } else { ### Proceed normally

    Ideally I would replace the ### Split here #### with  $html[$i] =~ /(.{500000,}?<\/html>/gs; (splitting every 500,000 characters) but this does not work since split is limited to int max (~32000 iirc). Splitting every 32000 characters results in far too many smaller strings

    Any suggestions as to how to get around this?

    Thanks, Colin

    edited for formatting

Best way to search file
4 direct replies — Read more / Contribute
by insta.gator
on Apr 15, 2015 at 11:44

    I am a super novice Perl programmer.

    I have 2 delimited files. File 1 is pipe delimited and contains names with social security numbers. File 2 is comma delimited and contains names, socials and a unique identifier that is alphanumeric. There is a max of 1 row of data for each person in file 2. There could be none. The is 1 or more rows of data for each person in file 1. Could be 1 or there may be 30. And the rows are typically interspersed throughout the file (not together). What I need to do is grab the SSN from file 1, find that SSN in file 2, grab the corresponding unique identifier from file 2 and plug it into a field in the record in file 1. I have a Perl script that is working but it is very, very slow. I am splitting the file 1 lines into an array, doing the lookups and replaces and then writing the line to a file. I repeat this until I process all the records in file 1.

    Sorry to be so long winded. Any suggestions on how to improve (speed up) would be greatly appreciated.

    I would be happy to post my code but it is probably too long for this small box.

How do I get to ignore special characters?
1 direct reply — Read more / Contribute
by amagana
on Apr 15, 2015 at 11:01

    The script stops and says su: incorrect password, these are Linux boxes at when it sends the passwords during the expect/send of su - root.

    Is it because I will have to escape the special characters when it starts to send the strings with special characters, I did not have to do this with Solaris boxes?

    Is there a way to just alway ignore special characters only during the send part of expect?

    1 #!/usr/bin/perl -w 2 3 use warnings; 4 use strict; 5 use Expect; 6 7 8 my $filename = "/var/tmp/expect_script.log"; 9 my $header = "\r\n\r\n======= system =======\r\n"; 10 my $timeout = 60; 11 12 13 #This will open a file and push it to array 14 my @servers; 15 16 #open (my $file, '<', $ARGV[0]) or die $!; 17 18 open (my $file, '<', "/home/amagana/scripts/lists/list_b2") or + die $!; 19 20 while(<$file>) { 21 push (@servers, $_); #push each line of the file to array 22 } 23 24 print "$_\n" for(@servers); 25 26 27 #This is an array 28 #my @servers = qw( 29 # 30 # 31 # 32 # 33 #); 34 35 36 for my $server (@servers) { 37 # do your thing with $server 38 39 change_password($server); 40 41 } 42 43 44 sub change_password { 45 46 my $system = shift; 47 my $ssh = Expect->new('ssh amagana@' . $system); 48 49 50 $ssh->debug(22); 51 $ssh->log_file("$filename"); 52 my $my_header = $header; 53 $my_header =~ s/system/$system/; 54 $ssh->print_log_file($my_header); 55 56 $ssh->expect ( $timeout, 57 [ qr/password:/], 58 [ qr/Are you sure you want to continue connecting \(yes\ +/no\)?/] 59 60 ); 61 62 if ($ssh->match() =~ m/Are you sure you want to continue conne +cting \(yes\/no\)?/ ) { 63 $ssh->send("yes\r"); 64 } 65 66 elsif ($ssh->match() =~ m/password:/ ) { 67 68 $ssh->send("mycurrentpassword\n"); #no problem with the specia +l characters at this line 69 70 } 71 72 $ssh->expect(60, '$'); 73 $ssh->send("su - root\n"); 74 $ssh->expect(60, 'Password:'); 75 $ssh->send( "rootpasswordwithspecialcharacters\n" ); #but my s +cript quits here 76 $ssh->expect(60, '#'); 77 $ssh->send("hostname\n"); 78 $ssh->expect(60, '#'); 79 $ssh->send("uptime\n"); 80 $ssh->expect(60, '#'); 81 $ssh->send("passwd amagana\n"); 82 $ssh->expect(60, 'New UNIX Password:'); #linux 83 $ssh->send( "mynewpasswordwithspecialcharacters\n" ); 84 $ssh->expect(60, 'Retype new UNIX password:'); #linux 85 $ssh->send( "mynewpasswordwithspecialcharacters\n" ); 86 $ssh->expect(60, '#'); 87 $ssh->send("exit\n"); 88 $ssh->expect(60, '$'); 89 $ssh->send("exit\n"); 90 $ssh->close(); 91 }

Add your question
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?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (6)
    As of 2015-04-19 20:22 GMT
    Find Nodes?
      Voting Booth?

      Who makes your decisions?

      Results (362 votes), past polls