Current location: Hot Scripts Forums » Programming Languages » Perl » Convert array PHP code to Perl


Convert array PHP code to Perl

Reply
  #1 (permalink)  
Old 08-02-05, 05:08 AM
FiRe FiRe is offline
Code Guru
 
Join Date: Oct 2004
Location: UK
Posts: 801
Thanks: 0
Thanked 0 Times in 0 Posts
Convert array PHP code to Perl

Can anyone convert this PHP code into perl?

PHP Code:

$offset_correction array_slice($num_days_last_array, -$offset$offset);

$new_count array_merge($offset_correction$num_days_array);
$offset_count count($offset_correction); 
thanks
__________________
Alexa Share <-- Trade virtual shares in websites with this online game.

codR.us <-- Submit and vote for your favorite code snippets with codR.us.

XEWeb.net <-- The ultimate PHP resource network.
Reply With Quote
  #2 (permalink)  
Old 08-02-05, 01:52 PM
Chas Chas is offline
Coding Addict
 
Join Date: Oct 2003
Location: California
Posts: 359
Thanks: 0
Thanked 0 Times in 0 Posts
I would imagine that splice[1] would take care of the first line but I'm not too familiar with PHP code. Start with this and go from there:

Code:
my @offset_correction = splice(@num_days_last_array, -$offset, $offset);
my @new_count = (@offset_correction, @num_days_array);
my $offset_count = @new_count;

# If you just need a count you *might* be able to get away with:
my $offset_count = (splice(@num_days_last_array, -$offset, $offset), @num_days_array));
~Charlie

[1] http://perldoc.perl.org/functions/splice.html
Reply With Quote
  #3 (permalink)  
Old 08-02-05, 03:23 PM
FiRe FiRe is offline
Code Guru
 
Join Date: Oct 2004
Location: UK
Posts: 801
Thanks: 0
Thanked 0 Times in 0 Posts
see thats wot someone else said, but array_slice and array_splice are 2 different things in PHP, so if you look at the description for array_slice in the PHP manual (http://php.net/array-slice) you will it does a different thing to splice...
__________________
Alexa Share <-- Trade virtual shares in websites with this online game.

codR.us <-- Submit and vote for your favorite code snippets with codR.us.

XEWeb.net <-- The ultimate PHP resource network.
Reply With Quote
  #4 (permalink)  
Old 08-02-05, 03:33 PM
Chas Chas is offline
Coding Addict
 
Join Date: Oct 2003
Location: California
Posts: 359
Thanks: 0
Thanked 0 Times in 0 Posts
Well you can use a simple array slice then:

Code:
my @new_arran = @old_array[-$offset .. $offset];
The only thing is the negative on $offset - that doesn't look right. What are sample values for $offset and what is your ultimate goal with this code?

~Charlie

Last edited by Chas; 08-02-05 at 03:50 PM.
Reply With Quote
  #5 (permalink)  
Old 08-02-05, 03:59 PM
Chas Chas is offline
Coding Addict
 
Join Date: Oct 2003
Location: California
Posts: 359
Thanks: 0
Thanked 0 Times in 0 Posts
According to the PHP docs:

Code:
$offset_correction = array_slice($num_days_last_array, -$offset, $offset);
it looks like that is just going to reverse your array, no? It really depends on the value of $offset though. In list context, splice will do the same as array_slice long as you only provide 3 args to the function. The fourth arg would be inserted at the splice point making it more like PHP's array_splice.

~Charlie
Reply With Quote
  #6 (permalink)  
Old 08-03-05, 02:58 AM
FiRe FiRe is offline
Code Guru
 
Join Date: Oct 2004
Location: UK
Posts: 801
Thanks: 0
Thanked 0 Times in 0 Posts
well this is how it determines the offset...

Code:
if ($month_start_day eq "Sun") { $offset = "0"; }
if ($month_start_day eq "Mon") { $offset = "1"; }
if ($month_start_day eq "Tue") { $offset = "2"; }
if ($month_start_day eq "Wed") { $offset = "3"; }
if ($month_start_day eq "Thu") { $offset = "4"; }
if ($month_start_day eq "Fri") { $offset = "5"; }
if ($month_start_day eq "Sat") { $offset = "6"; }
and this is what I came up with trying to solve the problem...

Code:
@offset_correction = @num_days_last_array[($#num_days_last_array-$offset) ... $#num_days_last_array];
push @offset_correction, @num_days_array;
$offset_count = @offset_correction;
but i dont thing its correct...
__________________
Alexa Share <-- Trade virtual shares in websites with this online game.

codR.us <-- Submit and vote for your favorite code snippets with codR.us.

XEWeb.net <-- The ultimate PHP resource network.
Reply With Quote
  #7 (permalink)  
Old 08-04-05, 10:49 AM
Chas Chas is offline
Coding Addict
 
Join Date: Oct 2003
Location: California
Posts: 359
Thanks: 0
Thanked 0 Times in 0 Posts
Can you explain in words the problem you are trying to solve? My problem is that I can't figure out exactly what you are trying to do with that snippet of code.

~Charlie
Reply With Quote
  #8 (permalink)  
Old 08-04-05, 01:31 PM
FiRe FiRe is offline
Code Guru
 
Join Date: Oct 2004
Location: UK
Posts: 801
Thanks: 0
Thanked 0 Times in 0 Posts
lol dont worry i recoded it instead thanks for trying!
__________________
Alexa Share <-- Trade virtual shares in websites with this online game.

codR.us <-- Submit and vote for your favorite code snippets with codR.us.

XEWeb.net <-- The ultimate PHP resource network.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP multi-dimensional array sorting issue aqw PHP 2 06-24-05 11:09 PM
How to sale php code to customer without giving him code pradeep_soft PHP 4 03-12-04 12:10 PM
linking to iframe not working :( j0d JavaScript 5 01-19-04 08:14 PM
convert perl code to php phptalk Perl 1 01-15-04 02:06 AM
Perl code : print "." x 20; - How to do it with PHP ? kevin PHP 2 07-04-03 04:29 AM


All times are GMT -5. The time now is 04:04 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.