Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Defining Array (Quick Question)

Status
Not open for further replies.

Alphabin

Programmer
Dec 8, 2002
119
CA
Hi,

I'm trying to extract dates (from flat file) which are in the following format: yyyy-mm-dd and then split the field to create an array... Here's a simplified version (no flat file) You will notice that only extract 3 dates out of 4 are extracted.

Thank you in advanced

Code:
while(<DATA>) {
my @Data=<DATA>;

	foreach my $record (@Data) {
	chop $record;

	# Split fields
	my @field=split(/\|/,$record);

	my @field_date=split(/\-/,$field[4]); # Date in field
	my @current_date = ($year, $month, $day); # Current date using localtime
	
	print "Field Date: @field_date <br> \n";
	
}

}
__DATA__
12|data|data|data|2004-06-01
13|data|data|data|2004-05-31
17|data|data|data|
14|data|data|data|2003-05-23
15|data|data|data|2002-02-12
 
The command
Code:
while (<DATA>)
read the first line of data. The command
Code:
my @Data=<DATA>
reads the remaining lines. The first line is not in your array : it was read before.

You could suppress the
Code:
while (<DATA>)
part or do the following change:
Code:
while([COLOR=green]defined(my $record[/color] = <DATA>[COLOR=green])[/color]) {
[COLOR=red][s]my @Data=<DATA>;

    foreach my $record (@Data) {[/s][/color]
    chop $record;

    # Split fields
    my @field=split(/\|/,$record);

    my @field_date=split(/\-/,$field[4]); # Date in field
    my @current_date = ($year, $month, $day); # Current date using localtime
    
    print "Field Date: @field_date <br> \n";
    
[COLOR=red][s]}[/s][/color]

}
__DATA__
12|data|data|data|2004-06-01
13|data|data|data|2004-05-31
17|data|data|data|
14|data|data|data|2003-05-23
15|data|data|data|2002-02-12

--------------------

Denis
 
bdchoulette:
Thank you for your help

But with your code I'm getting the following output

Code:
Field Date: 2004 06 01  
Field Date: 2004 05 31  
Field Date:   
Field Date: 2003 05 23  
Field Date: 2002 02 1
 
One thing to note, it might be better to use the chomp() function instead of chop().

chomp VARIABLE
chomp LIST
chomp This safer version of the chop entry elsewhere in this document removes any trailing string that corresponds to the current value of "$/" (also known as $INPUT_RECORD_SEPARATOR in the "English" module). It returns the total number of characters removed from all its arguments. It's often used to remove the newline from the end of an input record when you're worried that the final record may be missing its newline. When in paragraph mode ("$/ = """), it removes all trailing newlines from the string. When in slurp mode ("$/ = undef") or fixed-length record mode ("$/" is a reference to an integer or the like, see the perlvar manpage) chomp() won't remove anything. If VARIABLE is omitted, it chomps "$_". Example:

while (<>) {
chomp; # avoid \n on last field
@array = split(/:/);
# ...
}

If VARIABLE is a hash, it chomps the hash's values, but not its keys.

You can actually chomp anything that's an lvalue, including an assignment:

chomp($cwd = `pwd`);
chomp($answer = <STDIN>);

If you chomp a list, each element is chomped, and the total number of characters removed is returned.

- Rieekan
 
Code:
while (<DATA>) {
    chomp;
    my @record = split /\|/, $_;
    if ($record[4]) {
        my @field_date = split /-/, $record[4];
        print "Field Date: @field_date\n";
    }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top