Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

G'day brtch,

Welcome to the monastery.

Providing a little more context to your question would have been preferable. I'm assuming the first three fields are: month day hours:minutes:seconds. I'll leave you to extrapolate from there.

Perl has different operators for string and numerical comparisons. '==' is the numerical equality operator; 'eq' is for strings. See perlop for all the different operators; perlop - Equality Operators specifically discusses '==' and 'eq'.

How you go about breaking up your line for comparison will depend on how much detail you want (e.g. do you want to look at 'yy:yy:yy' as a whole or are you interested in the subfields). I see two main options you might pursue: using the split function or using a regular expression.

Using split can be as simple as:

$ perl -Mstrict -Mwarnings -E ' my $line = q{xxx yy yy:yy:yy xxx/xxxxx xxx xxxx xxx xxx}; my @fields = split / / => $line; say $fields[2]; ' yy:yy:yy

The problem with this level of simplicity is when further down your code you hit $fields[7] and have to backtrack to determine which field index 7 refers to. Ways around this include giving symbolic names to the indices or capturing each field into a meaningfully named variable:

$ perl -Mstrict -Mwarnings -E ' use constant { MONTH => 0, DAY => 1, TIME => 2, }; my $line = q{xxx yy yy:yy:yy xxx/xxxxx xxx xxxx xxx xxx}; my @fields = split / / => $line; say $fields[TIME]; ' yy:yy:yy
$ perl -Mstrict -Mwarnings -E ' my $line = q{xxx yy yy:yy:yy xxx/xxxxx xxx xxxx xxx xxx}; my ($month, $day, $time, $rest) = split / / => $line; say $time; ' yy:yy:yy

If you want to get at the subfields, then a regular expression solution might be better:

$ perl -Mstrict -Mwarnings -E ' my $line = q{xxx 1 12:34:56 xxx/xxxxx xxx xxxx xxx xxx}; my $line_re = qr{^(\w+) (\d+) (\d+):(\d+):(\d+) (.*)}; my ($month, $day, $hour, $min, $sec, $rest) = $line =~ m{$line_re}; say $hour; ' 12

All of those parts in parentheses are called Capture Groups. The link I've provided discusses these (as well as Named Capture Groups which I'll leave you to research if you're interested).

-- Ken

In reply to Re: Processing words in a file. by kcott
in thread Processing words in a file. by brtch

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (6)
    As of 2018-06-18 02:31 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (107 votes). Check out past polls.