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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

skip blanks, colons, and / 1

Status
Not open for further replies.

Porshe

Programmer
Jun 5, 2001
38
0
0
US
hi,

i'm trying to sort but i need to skip any spaces, colons and / in the line.

any help?

thanks!
 
ok- sorry...

my field is called 'time'
and it looks like this:


10/30/2001 12:30:00 p.m
10/29/2001 12:40:00 p.m
10/29/2001 12:40:00 a.m
10/03/2001 10:30:00 p.m
10/14/2001 1:00:00 a.m
10/30/2001 11:00:00 p.m

i need to sort it by time that means that if time is in ascending order (time-a) then it should sort by date and time. but the confusing part is how to get rid of all the
/ spaces and dots. it could be the same time but am and pm would make a big difference in how it gets sorted. so if it's asc. order i need to do am first then pm.

Any help would be appreciated!
 
The following should work:
Code:
@list=("10/30/2001 12:30:00  p.m",
"10/29/2001 12:40:00  p.m",
"10/29/2001 12:40:00  a.m",
"10/03/2001 10:30:00  p.m",
"10/14/2001 1:00:00  a.m",
"10/30/2001 11:00:00  p.m",
);
sub mysort() {
    @field1 = split (/\s+/,$a); # split on whitespace
    @date1 = split("/", $field1[0]); # split up date
    $time1 = $field1[1];
    $time1 =~ s/://g; # remove ':' from time
    $ampm1 = $field1[2];
    @field2 = split (/\s+/,$b);
    @date2 = split("/", $field2[0]);
    $ampm2 = $field2[2];
    $time2 = $field2[1];
    $time2 =~ s/://g;
    # ascending sort on year, month, day, am/pm, time                     
    $date1[2] <=> $date2[2] || $date1[0] <=> $date2[0] || $date1[1] <=> $date2[1] || $ampm2 cmp $ampm1 || $time1 <=> $time2;
}
@sorted = sort mysort @list;
 
Can you advise what the $a and $b represent in this script. I understand the splitting up of data and sorting but I am lost with these two variables. Are you storing the three fields in each one?

@field1 = split (/\s+/,$a);


@field2 = split (/\s+/,$b);
 
The variables $a and $b are the two elements to be compared. These variables are passed by reference into the sort subroutine when sort is called.
 
Still not familiar with the passing of references. I will need to start reading about references and structures.
 
raider: I don't think they're passed by reference, per se, they're passed by value. You can modify their values, but I don't think that will modify the original values. I think what you mean is they are &quot;implicitly passed&quot;, meaning you don't have to declare them as parameters. I think that they are scoped to include the sort statement and any sort routine(s) you might use, but nothing outside of that. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
tsdragon,

Your 'implicitly passed' explanation is a better one, but I do think that the references to $a and $b are passed. That's why you can't modify their original values. In the book &quot;Programming Perl&quot; in the description of sort says &quot;The variables $a and $b are passed by reference, so don't modify them in the subroutine&quot;.
 
oh right - so you *can* modify them then, you just shouldn't Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Thanks for the correction. I don't know WHY they would be passed that way - it sounds too dangerous to me, but I guess there's a reason for it. Since they have to be scalars anyway though, there's no reason I can think of. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
yeah - a bit funny Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
can anyone tell me if there is a module that could convert this:

10/30/2001 12:30:00 p.m

to epoch seconds and will it convert the p.m or a.m also into seconds?

thanks for the discussion above- it was really helpful!
 
Code:
#!/usr/local/bin/perl
use Time::Local;

$date = '10/30/2001 11:30:00  a.m';
print &quot;OLD Date: $date\n&quot;;

# convert to 24 hour clock.
# if last item starts with 'p' and the 4th item is not '12',
#  then add 12 to 4th item
my @date = split(/[\/ :]+/,$date);
if ($date[6] =~ /^p/i && $date[3] ne '12') 
    { $date[3] = $date[3] + 12; }

$seconds = timelocal($date[5],$date[4],$date[3],$date[1],$date[0]-1,$date[2]);

print &quot;Epoch Seconds: $seconds\n&quot;;

# check that it is correct.
$new_date = localtime($seconds);
print &quot;NEW Date: $new_date\n&quot;;

HTH If you are new to Tek-Tips, please use descriptive titles, check the FAQs,
and beware the evil typo.
 
i tried goBoating's script and i get
an error:

Month '-1' out of range 0..11

i used the same data. i don't get it?
 
Oaklander,

'epoch'- &quot;A particular period of history, especially one
considered remarkable or noteworthy.&quot;

In this context, that &quot;noteworthy&quot; period is the time
since January 1, 1970.

If you do
$time = time;
print &quot;$time\n&quot;;

the number you get back was the number of non-leap
seconds since 1/1/1970, or the length of the Perl epoch.
In this function, the Perl language is rather self-absorbed.


Porsche, I'll take a look. I've gotta' leave pretty soon so I don't know if I'll have an answer before tomorrow.
If you are new to Tek-Tips, please use descriptive titles, check the FAQs,
and beware the evil typo.
 
Regarding the statement: &quot;In this function, the Perl language is rather self-absorbed.&quot; PERL isn't being self-absorbed, just Unix-centric. Unix operating systems introduced the 1-1-1970 epoch. That's one of the reasons most unix operating systems didn't have to worry much about Y2K issues. The real problem for unix comes in (as I recall) 2017, when the counter used to hold all those seconds overflows.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
I sit corrected. ;-) If you are new to Tek-Tips, please use descriptive titles, check the FAQs,
and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top