Shlomi,

See far bottom for my updated code. 


Chris Stinemetz 

-----Original Message-----
From: Shlomi Fish [mailto:shlo...@iglu.org.il] 
Sent: Tuesday, February 01, 2011 4:18 AM
To: beginners@perl.org
Cc: Chris Stinemetz
Subject: Re: sorting report

Hi Chris,

a few comments on your code:

On Tuesday 01 Feb 2011 06:43:43 Chris Stinemetz wrote:
> I would like to sort my final report in the following order:
> 
> $data[31],$data[32],$data[38]
> 
> How would I add this into my following program to get the report sorted
> this way?
> 
> Thanks in advance.
> 
> Chris
> 
> #!/usr/bin/perl
> 
> use warnings;
> #use strict;

Why is "use strict;" commented out? It should be active.

> use FileHandle;

Why are you using the FileHandle module? It is deprecated.

> use IO::Handle;
> 

Yes, IO::Handle is better.

> RAW->format_lines_per_page(10000000000);

This number will overflow the integer word on perls with 32-bit integers.

> 
> 
> format RAW_TOP =
> @|||||||||||||||||||||||||||||||||||||||
> "######--> Smart Phone report. <--######",
> Market      ESN/MIN          Mobile         Cell    Sector    Carrier    
> Bytes
> ==========================================================================
> ============== .
> 

Don't use perlform:

http://perl-begin.org/tutorials/bad-elements/#perlform

> 
> format RAW =
> @<<<<<<<< @|||||||||||||| @|||||||||||||| @|||||||| @|||||||| @|||||||
> @>>>>>>>> $mkt,$mtype,$smartPhone,$cell,$sector,$carrier,$rlptxat
> .
> 
> # SmartPhone type Hash based on ESN or MEID HEX number
> my %smartPhone = (
>    "CURVE850a" => { start => "a000001ca64E38",
>                    end   => "a00000255c29c0", },
>    "KYOM6000" => { start => "a0000012b71b00",
>                    end   => "a0000012fef1a0", },
>    "CURVE850" => { start => "ffffffff001388",
>                    end   => "ffffffff001770", },
> #   "Huawei"   => { start => "a00000130fa7d0",
> #                   end   => "ffffffff001770", },
> );
> 
> #### Market assignment Hash based on cell number
> my %marketInfo = (
>     MCI => { start => 1,
>              end   => 299, },
>     STL => { start => 300,
>              end   => 599, },
>     ICT => { start => 800,
>              end   => 850, },
> );
> 
> sub getSmartPhone
> {
> 
>    my $val = shift;
>    foreach my $k (keys %smartPhone)
>    {
>       my ($start, $end) = @{$smartPhone{$k}}{qw/start end/};
>       return $k if $start ge $val and $val le $end;
>    }
> 
>    return "";
> }

This algorithm will work fine if %smartPhone is small, but is not very 
efficient. You may wish to look at making an array (or a binary search tree) 
of start/end pairs and doing a binary search if it gets very large.

> 
> 
> sub getMarket
> {
> 
>    my $val = shift;
>    foreach my $k (keys %marketInfo)
>    {
>      my ($start, $end) = @{$marketInfo{$k}}{qw/start end/};
>      return $k if $start <= $val and $val <= $end;
>    }
> 
>    return "";
> }



> 
> open(RAW, ">test.rpt");

Use three-args-open and lexical filehandles.

> while (<>) {

You should iterate using an explicit variable:

while (my $line = <>)

Regards,

        Shlomi Fish

>    chomp;
>    if (/;/) {
>       @data = split /;/;
>    if ($data[31] =~ m/^-?\d+$/) {  #### regular expression for real
> numerical value $mkt = getMarket($data[31]);
>    }
>    else
>    {
>       $mkt = "";
>    }
> 
>    if ( length($data[5]) > 12) {
>       $smartPhone = getSmartPhone(substr($data[5],2,14));
>    }
>    else
>    {
>       $smartPhone = "";
>    }
> 
> 
>      ($mtype,$cell,$sector,$carrier,$rlptxat) =
> ($data[5],$data[31],$data[32],$data[38],$data[44]); #     print "$mkt\t 
> $mtype\t $smartPhone\t  $cell\t  $sector\t  $rlptxat\n"; write(RAW);
>    }
> }
> select(RAW);
> close(RAW);

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Stop Using MSIE - http://www.shlomifish.org/no-ie/

Chuck Norris can make the statement "This statement is false" a true one.

Please reply to list if it's a mailing list post - http://shlom.in/reply .




#!/usr/bin/perl

use warnings;
use strict;
use IO::Handle;

RAW->format_lines_per_page(10000000000); # I will change this once I get strict 
pragma to work.


format RAW_TOP =
@|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
 "######--> SMART PHONE REPORT <--######",
 Market      ESN/MEID         Mobile        Cell     Sector    Carrier       
Bytes
--------    ----------       --------      ------   --------  ---------     
-------
.


format RAW =
@|||||||| @|||||||||||||| @|||||||||||||| @||||||| @|||||||| @|||||||||| 
@|||||||||||||
my ($mtype,$smartPhone,$cell,$sector,$carrier,$rlptxat)
.

# SmartPhone type Hash based on ESN or MEID HEX number # I will change this 
once I get strict pragma to work.
my %smartPhone = (
   "CURVE850a" => { start => "a000001ca64E38",
                   end   => "a00000255c29c0", },
   "KYOM6000" => { start => "a0000012b71b00",
                   end   => "a0000012fef1a0", },
   "CURVE850" => { start => "ffffffff001388",
                   end   => "ffffffff001770", },
#   "Huawei"   => { start => "a00000130fa7d0",
#                   end   => "ffffffff001770", },
);

#### Market assignment Hash based on cell number
my %marketInfo = (
    MCI => { start => 1,
             end   => 299, },
    STL => { start => 300,
             end   => 599, },
    ICT => { start => 800,
             end   => 850, },
);

sub getSmartPhone
{

   my $val = shift;
   foreach my $k (keys %smartPhone)
   {
      my ($start, $end) = @{$smartPhone{$k}}{qw/start end/};
      return $k if $start ge $val and $val le $end;
   }

   return "";
}


sub getMarket
{

   my $val = shift;
   foreach my $k (keys %marketInfo)
   {
     my ($start, $end) = @{$marketInfo{$k}}{qw/start end/};
     return $k if $start <= $val and $val <= $end;
   }

   return "";
}

open(RAW, ">test.rpt");
while (<>) {
   chomp;
   if (/;/) {
      my @data = split /;/;
   if ($data[31] =~ m/^-?\d+$/) {  #### regular expression for real numerical 
value
      my $mkt = getMarket($data[31]);
   }
   else
   {
      my $mkt = "";
   }

   if ( length($data[5]) > 12) {
      my $smartPhone = getSmartPhone(substr($data[5],2,14));
   }
   else
   {
      my $smartPhone = "";
   }


     my ($mtype,$cell,$sector,$carrier,$rlptxat) = 
($data[5],$data[31],$data[32],$data[38],$data[44]);
#     print "$mkt\t  $mtype\t $smartPhone\t  $cell\t  $sector\t  $rlptxat\n";
   write(RAW);
   }
}


select(RAW);
close(RAW);

When I use the strict pragma I have the following warning and nothing is 
outputted to the report 'test.rpt'.

Once I get this resolved I will take your recommendations from original email 
and apply them to my program. 

I hope you can help I know the importance of using strict pragma and would be 
very happy to get it to work. 

I have declared all my variables after enabling strict pragma.

Warnings I get:
Not enough format arguments at ./io.pl line 24, <> line 481.
Use of uninitialized value $mtype in formline at ./io.pl line 24, <> line 482.
Use of uninitialized value $smartPhone in formline at ./io.pl line 24, <> line 
482.
Use of uninitialized value $cell in formline at ./io.pl line 24, <> line 482.
Use of uninitialized value $sector in formline at ./io.pl line 24, <> line 482.
Use of uninitialized value $carrier in formline at ./io.pl line 24, <> line 482.
Use of uninitialized value $rlptxat in formline at ./io.pl line 24, <> line 482.
Not enough format arguments at ./io.pl line 24, <> line 482.
Use of uninitialized value $mtype in formline at ./io.pl line 24, <> line 483.
Use of uninitialized value $smartPhone in formline at ./io.pl line 24, <> line 
483.
Use of uninitialized value $cell in formline at ./io.pl line 24, <> line 483.
Use of uninitialized value $sector in formline at ./io.pl line 24, <> line 483.
Use of uninitialized value $carrier in formline at ./io.pl line 24, <> line 483.
Use of uninitialized value $rlptxat in formline at ./io.pl line 24, <> line 483.
Not enough format arguments at ./io.pl line 24, <> line 483.
Use of uninitialized value $mtype in formline at ./io.pl line 24, <> line 484.
Use of uninitialized value $smartPhone in formline at ./io.pl line 24, <> line 
484.
Use of uninitialized value $cell in formline at ./io.pl line 24, <> line 484.
Use of uninitialized value $sector in formline at ./io.pl line 24, <> line 484.
Use of uninitialized value $carrier in formline at ./io.pl line 24, <> line 484.
Use of uninitialized value $rlptxat in formline at ./io.pl line 24, <> line 484.
Not enough format arguments at ./io.pl line 24, <> line 484.
Use of uninitialized value $mtype in formline at ./io.pl line 24, <> line 485.
Use of uninitialized value $smartPhone in formline at ./io.pl line 24, <> line 
485.
Use of uninitialized value $cell in formline at ./io.pl line 24, <> line 485.
Use of uninitialized value $sector in formline at ./io.pl line 24, <> line 485.
Use of uninitialized value $carrier in formline at ./io.pl line 24, <> line 485.
Use of uninitialized value $rlptxat in formline at ./io.pl line 24, <> line 485.
Not enough format arguments at ./io.pl line 24, <> line 485.


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to