Loading ...
Sorry, an error occurred while loading the content.

split question...

Expand Messages
  • Jaeho Kim
    How can I separate name value pairs if one of the values contains spaces? Ex: $line = ifIndex=222 sysDescr= I am a PC ifOperStatus=2 into: ifIndex=222
    Message 1 of 3 , Dec 3, 2003
    • 0 Attachment
      How can I separate name value pairs if one of the values contains
      spaces?

      Ex:

      $line = 'ifIndex=222 sysDescr="I am a PC" ifOperStatus=2'

      into:

      ifIndex=222

      sysDescr="I am a PC"

      ifOperStatus=2
    • Damien Carbery
      ... From the following regex I got it to work reasonably well. It assumes name is always made up of characters. @result = split( / b( w+)=/, $line ); It
      Message 2 of 3 , Dec 4, 2003
      • 0 Attachment
        --- In perl-beginner@yahoogroups.com, "Jaeho Kim" <jaehokim1@m...> wrote:
        > How can I separate name value pairs if one of the values contains
        > spaces?
        >
        > Ex:
        >
        > $line = 'ifIndex=222 sysDescr="I am a PC" ifOperStatus=2'
        >
        > into:
        >
        > ifIndex=222
        >
        > sysDescr="I am a PC"
        >
        > ifOperStatus=2

        From the following regex I got it to work reasonably well.
        It assumes 'name' is always made up of characters.
        @result = split( /\b(\w+)=/, $line );

        It returns:
        $result[0]=
        $result[1]=ifIndex
        $result[2]=222
        $result[3]=sysDescr
        $result[4]="I am a PC"
        $result[5]=ifOperStatus
        $result[6]=2

        You'd have to experiment to see if the first element was always blank.
        It is easy to strip off the quotes ( one way: s/^"(.+)"$/$1/ ) if they
        exist.
        You could probably put the convert the above list into a hash for
        easier referencing of the data.

        And, of course, there's probably a better way to do it.
      • Ben Maynard
        Jaeho Can you not add another delimiter? ie: ifIndex=222~sysDescr= I am a PC ~ifOperStatus=2 If so then you can do a split. split /~/, $line That would be
        Message 3 of 3 , Dec 5, 2003
        • 0 Attachment
          Jaeho

          Can you not add another delimiter?

          ie:

          'ifIndex=222~sysDescr="I am a PC"~ifOperStatus=2'

          If so then you can do a split.

          split /~/, $line

          That would be the easiest method, if you can't do that
          and your line will always take the same format eg:

          key=value key="value" key=value

          Then a regular expression will do the job.

          HTH

          Ben

          --- Jaeho Kim <jaehokim1@...> wrote: > How can I
          separate name value pairs if one of the
          > values contains
          > spaces?
          >
          > Ex:
          >
          > $line = 'ifIndex=222 sysDescr="I am a PC"
          > ifOperStatus=2'
          >
          > into:
          >
          > ifIndex=222
          >
          > sysDescr="I am a PC"
          >
          > ifOperStatus=2
          >
          >
          >
          >
          > ------------------------ Yahoo! Groups Sponsor
          >
          > Unsubscribing info is here:
          > http://help.yahoo.com/help/us/groups/groups-32.html
          >
          > Your use of Yahoo! Groups is subject to
          > http://docs.yahoo.com/info/terms/
          >
          >

          ________________________________________________________________________
          Download Yahoo! Messenger now for a chance to win Live At Knebworth DVDs
          http://www.yahoo.co.uk/robbiewilliams
        Your message has been successfully submitted and would be delivered to recipients shortly.