On 2011.10.15.00.08, Chris Stinemetz wrote:
> My scripts thus far:
> 
> heh.pl
> #!/usr/bin/perl
> 
> use warnings;
> use strict;
> requuire "heh.lib";

Couple things here -- "require" is spelled wrong. But also you have to
say "heh.lib.pl". I will let someone else suggest that you organize this
using modules (.pm files and "use" statements).

> open my $fh, '<', $filepath or die "ERROR opening $filepath: $!";
> open my $out, '>', $runTime or die "ERROR opening $runTime: $!";

Nice use of 3-arg open and error handling.

>   if (/,HEH/);

I believe you mean:

  if (/, HEH/) { ... }

with a space after the comma. (my "..." is just a placeholder for what
you need to do in there).

Are all the data you want to extract always contained on this one line?

> print $out form
> "                        ^>>>>>> HR 17-21 HEH Report @<<<<<<<<<<<<<<<<<<",
> 
> "  Cell      CBR      CDM1     CDM2       CDM3     TFU1     TFU2
> CCU     EVM     TXAMP     CTRM  ",
> "--------- -------- -------- --------- --------- -------- ---------
> -------- -------- -------- --------";

Formats! Neat. I've only used those once In Real Life.

> heh_lib.pl
> 
> #!/usr/bin/perl
> 
> use warnings;
> use strict;
> 
> sub getCell {
>   my $str = "REPT:CELL, HEH";
> }

OK -- so I think you want to call getCell on each line that contains
", HEH" eh? So what you want to do is pass the line into getCell for it
to pull stuff out of, and then get the good bits back out.

I wasn't entirely clear on what you want to get back out, but here is an
example:

  my $input_line = "00 REPT:CELL 20 CDM 1, CRC, HEH\n";

  my ($cell, $cdm) = getCell($input_line);

  print "Cell: $cell CDM: $cdm\n";

  sub getCell {
    my ($input_line) = @_; # gets the $input_line parameter
    if($input_line =~ /Cell: (\d+) CDM (\d+),/) {
      # We found something that looks like "Cell: XXX CDM YYY,"
      # These are in parenthesis, so go into $1 and $2
      my $cell = $1;
      my $cdm = $2;
      return ($cell, $cdm);
      # I could have done: return ($1, $2)
    } else {
      die "BAD LINE: $input_line\n";
    }
  }

Since this is a teaching list -- I ran this and got:

  BAD LINE: 00 REPT:CELL 20 CDM 1, CRC, HEH

Digression: So... that didn't work. I then noticed that my regex says
"Cell:" but the output (and the input) say "CELL". Not only does mine
have a ":" when it should't, but also regexes are case-sensitive by
default. I changed my regex to use "CELL"
  
  my $input_line = "00 REPT:CELL 20 CDM 1, CRC, HEH\n";

  my ($cell, $cdm) = getCell($input_line);

  print "Cell: $cell CDM: $cdm\n";

  sub getCell {
    my ($input_line) = @_; # gets the $input_line parameter
    if($input_line =~ /CELL (\d+) CDM (\d+),/) {
      # We found something that looks like "Cell: XXX CDM YYY,"
      # These are in parenthesis, so go into $1 and $2
      my $cell = $1;
      my $cdm = $2;
      return ($cell, $cdm);
      # I could have done: return ($1, $2)
    } else {
      die "BAD LINE: $input_line\n";
    }
  }

And now I get:

  Cell: 20 CDM: 1

This will be a bit more complex if you need to extract data from other
lines as well.

--Brock


-- 
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