RE: More string manipulation

2002-02-15 Thread Timothy Johnson


You could try making an array or hash with the months(I used an array
because the index is already numeric):

   @mons =
('Nul','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','De
c');

   #convert the date
   $threeLetterMonth = $mons[$record->{month}];


-Original Message-
From: Brian Johnson [mailto:[EMAIL PROTECTED]]
Sent: Friday, February 15, 2002 9:20 AM
To: [EMAIL PROTECTED]
Subject: More string manipulation


I have the following code that I need a little advice on.

The $record->{month} returns the month in integer format (ie 1, 2, 3), I
need to change it to to a three letter string (ie Jan, Feb, Mar)

  foreach $item (@items) {
my $record;
my $test;
foreach $record (@{$PDB->{records}}) {
  # look for date, from, or subject
  # XXX add date/time check
  # Skip everything except the Inbox folder
  next if ($record->{category} ne $cat);
  my $emaildate = "$record->{day} $record->{month} $record->{year}
$record->{hour}:$record->{minute}";
  if ( $record->{subject} eq $item->{Subject} &&
   $record->{from} eq $item->{From} &&
   $item->{Date} =~ $emaildate) {
#do something
if( $record->{is_read} ne $item->{is_read}) {
  $record->{is_read} = $item->{is_read};
  $record->{attributes}{dirty}=1;
}
  }
}
  }


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



This email may contain confidential and privileged 
material for the sole use of the intended recipient. 
If you are not the intended recipient, please contact 
the sender and delete all copies.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: More string manipulation

2002-02-15 Thread Andrea Holstein

In article <00bb01c1b645$0b8eef80$[EMAIL PROTECTED]> wrote "Brian Johnson"
<[EMAIL PROTECTED]>:

> I have the following code that I need a little advice on.

It'e easier for us all, 
if you short describe your problem.
I assume that the following contains some errors,
you can't find.

> 
> The $record->{month} returns the month in integer format (ie 1, 2, 3), I need to 
>change it to to a
> three letter string (ie Jan, Feb, Mar)

There are sure some Date:: modules absolving this job.
Unless them a simple array will convert:

my @month_long = (undef, qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/);

now $month_long[4] contains 'Apr'.

> 
>   foreach $item (@items) {
> my $record;

Declare your variables, where you need them.

> my $test;

Where do you need $test;

> foreach $record (@{$PDB->{records}}) {
  ^^
  my $record

>   # look for date, from, or subject
>   # XXX add date/time check
>   # Skip everything except the Inbox folder
>   next if ($record->{category} ne $cat);
>   my $emaildate = "$record->{day} $record->{month} $record->{year}
> $record->{hour}:$record->{minute}";
>   if ( $record->{subject} eq $item->{Subject} &&
>$record->{from} eq $item->{From} &&
>$item->{Date} =~ $emaildate) {
  ^^
   looks dangerous, didn't you mean $item->{Date} eq /$emaildate/;
> #do something
> if( $record->{is_read} ne $item->{is_read}) {
>   $record->{is_read} = $item->{is_read};
>   $record->{attributes}{dirty}=1;
> }
>   }
> }
>   }

I find the code hard to read, because there are so many $record assignments.
I would shorten the code to:

my @month_long = (undef, qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/);
foreach my $item (@items) {
foreach (grep {$_->{category} eq $cat} @{$PDB->{records}}) {
my $emaildate = 
"$_->{day} $month_long[$_->{month}] $_->{year} $_->{hour}:$_->{minute}";
if ($_->{subject} eq $item->{Subject} &&
$_->{from} eq $item->{From} &&
$_->{Date} eq $emaildate) {

# do something

}
}
}

Best Wishes,
Andrea


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]