Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Changing the element of prior array records.

teletayoteletayo Posts: 1Member
I have the following code that pass the contents of a test file into array of records.
Here is the format of each record:
Name assignment Effective Date End date:
James monitor 12-mar-2000 13-oct-2000
James water 13-apr-2001 14-june-2001
James florring 15-aug-2001 10-dec-2001

My aim is to acomplish two things:
a)Modify the end date of each record(record2 for example)so that its end date = effective date of next record(record3) -1. That is, End Date of record2 would change to 14-aug-2001. End Date of record1: 12-apr-2001. End Date of record3 would still be 10-dec-2001.

b)Possibly, if required to do it in opposite direction. That is end date of record1 would remain the same, end date of record2: 11-mar-2000(effective date of record1 - 1) and end date of record3:12-apr-2001.

Please Help in module "Modify efective date here".

#!/usr/bin/perl
use strict ;
#use warnings ;
use Date::Manip ;

my (@input) ;
my ($input_record) ;
my ($input_file) ;
my ($output_file);
my ($array_record);
my (@array_record);
my (@array) ;
my ($name);
my ($date);
my (@array_file);
my (@names);
my ($new_record);
my ($backdt_hst) ;
my ($backdt_crt) ;
my ($empid_hld) ;

$input_file = "test.txt" ;
$output_file = "hist_out.txt";

#check input file
(-e $input_file) || die ("input file does not exist
") ;
open(INPUT, "$input_file") || die ("could not open infile
");
open(OUTPUT, ">$output_file") || die ("could not open outfile
") ;

#send records to array file from input file
@array_file = ;

#split each record into array elements
foreach $array_record (@array_file){
@array = split (",", $array_record) ;

#Modify End Date here
I am stumped please help!

#stop modifying here and start joining back each of the elements to form array records
$new_record = join ',', @array;

#put back all records into ouput file
print OUTPUT "$new_record
";

}
close (INPUT) ;
close (OUTPUT) ;

Comments

  • JonathanJonathan Posts: 2,914Member
    : #split each record into array elements
    : foreach $array_record (@array_file){
    : @array = split (",", $array_record) ;
    :
    With iteration so easily available, I guess it's easy to forget that arrays are indexable. :-) How about something like:-

    for my $i (0..$#array_file) {
    $array_record = $array_file[$i];

    You can then access the previous and last records (with appropraite bound checking) with:-

    $array_record = $array_file[$i - 1]; # last
    $array_record = $array_file[$i + 1]; # next

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Sign In or Register to comment.