wind direction in a string 
Author Message
 wind direction in a string

Here's something to think about...

A string contains for example: "wind direction = 034 degrees"
How do I replace a range between 000 and 045 to NNE so that the output would
look like: "wind direction = NNE"

Thanks in advance!



Mon, 30 Aug 2004 00:45:46 GMT  
 wind direction in a string

Quote:
> Here's something to think about...

> A string contains for example: "wind direction = 034 degrees"
> How do I replace a range between 000 and 045 to NNE so that the output would
> look like: "wind direction = NNE"

I would do this:


my $example = "wind direction = 034 degrees";
my ($angle) = ($example =~ /wind direction = (\d+) degrees/);

# convert the angle to a number between 0 and 7. if it's an int,
# don't do anything. if it's a floating point number, round up 1,
# and truncate
my $dir = $angle / 45;
$dir = int($dir + 1) unless ($dir == int($dir));
print "wind direction = $directions[$dir]\n";

There might be an easier way to do it, but I'd say this way's pretty damn
good.

HTH,
Josh
------------------------------------------------------------------

Webmaster, WRUW-FM < http://www.*-*-*.com/ ;
Postmaster, The Geek Empire < http://www.*-*-*.com/ ;

"My parents just came back from a planet where the {*filter*} lifeform
 had no bilateral symmetry, and all I got was this stupid F-Shirt."
------------------------------------------------------------------



Mon, 30 Aug 2004 02:39:07 GMT  
 wind direction in a string

Quote:

> Here's something to think about...

> A string contains for example: "wind direction = 034 degrees"
> How do I replace a range between 000 and 045 to NNE so that the output would
> look like: "wind direction = NNE"

> Thanks in advance!

if i get where you're going with this, this solution'll prolly set you up:

(i like the little bit of "perl magic" that the subroutine uses)

$_ = 'wind direction = 034 degrees';

s/(\d+\s+degrees)/newdir($1)/e;

print "$_\n";

sub newdir {
 my $dir = shift;

 # you'll undoubtedly want more than one here...
 %dirs = ('NNE' => [0,45]);

 foreach (keys %dirs) {
  return $_ if ($dirs{$_}->[0] <= $dir && $dir <= $dirs{$_}->[1]);
 }

 # if we get this far, just return what was passed in...
 return $dir;

Quote:
}

hth-
--
Michael Budash
Michael Budash Consulting



Mon, 30 Aug 2004 02:42:39 GMT  
 wind direction in a string

Quote:

> Here's something to think about...

> A string contains for example: "wind direction = 034 degrees"
> How do I replace a range between 000 and 045 to NNE so that the output would
> look like: "wind direction = NNE"

Wouldn't NNE be between 11.25 and 33.75 degrees?

Regards,

Ian



Mon, 30 Aug 2004 05:56:02 GMT  
 wind direction in a string

Quote:

> > Here's something to think about...

> > A string contains for example: "wind direction = 034 degrees"
> > How do I replace a range between 000 and 045 to NNE so that the output would
> > look like: "wind direction = NNE"

> I would do this:



You are missing a few compass points there.


NNW);

John
--
use Perl;
program
fulfillment



Mon, 30 Aug 2004 08:10:33 GMT  
 wind direction in a string

Quote:


> > Here's something to think about...

> > A string contains for example: "wind direction = 034 degrees"
> > How do I replace a range between 000 and 045 to NNE so that the output would
> > look like: "wind direction = NNE"

> Wouldn't NNE be between 11.25 and 33.75 degrees?

You are correct sir!

                 0
                360

                 N
          NNW    |    NNE
    315          |          45
       NW        |        NE
                 |
    WNW          |          ENE
                 |
270 W -----------+----------- E 90
                 |
    WSW          |          ESE
                 |
       SW        |        SE
    225          |          135
          SSW    |    SSE
                 S

                180

:-)
John
--
use Perl;
program
fulfillment



Mon, 30 Aug 2004 09:02:25 GMT  
 wind direction in a string
Great! It works. I added an extra N to the array because 351  / 22.5 = 15.6
(16)
so the final array looks like:

Thank you very much!



Quote:

> > > Here's something to think about...

> > > A string contains for example: "wind direction = 034 degrees"
> > > How do I replace a range between 000 and 045 to NNE so that the output
would
> > > look like: "wind direction = NNE"

> > I would do this:


> You are missing a few compass points there.


> NNW);

> John
> --
> use Perl;
> program
> fulfillment



Mon, 30 Aug 2004 19:26:25 GMT  
 wind direction in a string

  EM> Great! It works. I added an extra N to the array because 351  / 22.5 = 15.6
  EM> (16)

ever heard of the modulus function %?

  EM> so the final array looks like:

redundant use of N as i mention above.

uri

--

-- Stem is an Open Source Network Development Toolkit and Application Suite -
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org



Mon, 30 Aug 2004 19:29:55 GMT  
 wind direction in a string

Quote:

> There might be an easier way to do it, but I'd say this way's pretty damn
> good.

... except it's wrong ;-)

# compass.pl - turn numerical angles into compass points

sub points {
    my $point = int ((($_[0] + 11.25) % 360) / 22.5);
    (qw/N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW/)[$point];

Quote:
}

for (0..360) {
    print "$_ -> ", points ($_), "\n";

Quote:
}

-- Dave Tweed


Mon, 30 Aug 2004 20:14:30 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. regular expression to describe 16 degrees of wind direction

2. Newbie Help/Advice/Direction

3. Please advise on directions

4. Point me in the right direction

5. Beginners question - directions please...

6. Odd problem with output re-direction

7. Newbie looking for direction

8. command-line output re-direction in winNT

9. Looking for directions : spawning independent CGIs

10. point me in the right direction

11. Please advise on directions

12. Looking for the right direction

 

 
Powered by phpBB® Forum Software