in reply to How to split unique patterns

Instead of splitting, I would instead match the data I want to keep.

Using a regular expression has the benefit of also somewhat validating your data, so you get notice of malformed input early:

my @columns= qw( name branch code timestamp info1 info2 id); $line=~ /^(A)::(B):(123-456) ([012]\d:[0-6]\d):(C)::(D):(789)/ or die "Malformed input [$line] in line $."; my %info; @info{ @columns }= ($1,$2,$3,$4,$5,$6);

Replies are listed 'Best First'.
Re^2: How to split unique patterns
by reisinge (Hermit) on Jun 10, 2013 at 14:15 UTC

    Didn't you mean @info{ @columns }= ($1,$2,$3,$4,$5,$6) (a hash slice) instead of $info{ @columns }= ($1,$2,$3,$4,$5,$6)?

    Well done is better than well said. -- Benjamin Franklin

      Whoops! Thank you, fixed!

Re^2: How to split unique patterns
by cornelius80 (Initiate) on Jun 10, 2013 at 08:55 UTC
    Hi Corion, Thank you for your suggestion. However, I might have left out some criteria though. 1. A,B,C,D.. are headers that I would need to have with their preceding values equated to. eg. A=undef,B=123-456 hh:mm, C=undef,D=789... 2. This would mean that the RHS of the headers would change over time keeping the LHS headers constant. How would you suggest that I overcome this, please? Thank you. Kind Regards, Cornelius

      I don't understand the additional requirements from your text. Can you maybe post some (anonymized) more relevant input data?

        Hi Corion, Sorry for the misunderstanding. Here goes... I have a string as follows that I will collate into an array: info::gmdate:2013-06-07 05:57:tccat_cico::r From that array, I am intending to split the data based on ':' such that I get the following, info gmdate 2013-06-07 05:57 tccat_cico r However, the "2013-06-07 05:57" gets separated into 2013-06-07 05 57 which is not what is intended.
      ... I might have left out some criteria ...

      Oh, of course you left out critical criteria! Answering these questions would not be near as much fun if we actually had accurate problem statements to begin with. I'm sure Corion appreciates the opportunity to waste... er, devote his or her time to providing a useful and insightful answer to a fundamentally mis-stated question.

      Many ++ to Corion for truly humble monkish patience, forbearance and generosity in dealing with a miserable sinner.