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

Re: [PBML] Converting a string into a list

Expand Messages
  • Arun Pandey
    I think you should use eval function, i.e create a code dynamicaly using the string and then eval it. ... From: brendangibson To:
    Message 1 of 4 , Aug 2, 2006
    • 0 Attachment
      I think you should use eval function, i.e create a code dynamicaly using
      the string and then eval it.
      ----- Original Message -----
      From: "brendangibson" <brendangibson@...>
      To: <perl-beginner@yahoogroups.com>
      Sent: Wednesday, August 02, 2006 5:58 PM
      Subject: [PBML] Converting a string into a list


      > If I have a string like '1..10,12..20', how can I use that as a list?
      >
      > I want to do something like:
      >
      > my $string = '1..10,12..20'; # Is read from a file, so must be a string
      > my $value = 9;
      >
      > my @pattern = convert_string_to_list($string);
      >
      > if (grep(/^$value$/,@pattern)) {
      > print "matched\n";
      > }
      > else {
      > print "didn't match\n";
      > }
      >
      > How can I do the bit of "convert_string_to_list"? I could do it with
      > split if my string was just a comma separated list, but the range
      > operator makes this tricky? Is there an easy way?
      >
      >
      >
      >
      >
      >
      > Unsubscribing info is here:
      http://help.yahoo.com/help/us/groups/groups-32.html
      > Yahoo! Groups Links
      >
      >
      >
      >
      >
      >
      >
    • brendangibson
      Genius! ... using ... string
      Message 2 of 4 , Aug 2, 2006
      • 0 Attachment
        Genius!




        --- In perl-beginner@yahoogroups.com, "Arun Pandey" <arunp@...> wrote:
        >
        > I think you should use eval function, i.e create a code dynamicaly
        using
        > the string and then eval it.
        > ----- Original Message -----
        > From: "brendangibson" <brendangibson@...>
        > To: <perl-beginner@yahoogroups.com>
        > Sent: Wednesday, August 02, 2006 5:58 PM
        > Subject: [PBML] Converting a string into a list
        >
        >
        > > If I have a string like '1..10,12..20', how can I use that as a list?
        > >
        > > I want to do something like:
        > >
        > > my $string = '1..10,12..20'; # Is read from a file, so must be a
        string
        > > my $value = 9;
        > >
        > > my @pattern = convert_string_to_list($string);
        > >
        > > if (grep(/^$value$/,@pattern)) {
        > > print "matched\n";
        > > }
        > > else {
        > > print "didn't match\n";
        > > }
        > >
        > > How can I do the bit of "convert_string_to_list"? I could do it with
        > > split if my string was just a comma separated list, but the range
        > > operator makes this tricky? Is there an easy way?
        > >
        > >
        > >
        > >
        > >
        > >
        > > Unsubscribing info is here:
        > http://help.yahoo.com/help/us/groups/groups-32.html
        > > Yahoo! Groups Links
        > >
        > >
        > >
        > >
        > >
        > >
        > >
        >
      • Charles K. Clarkson
        ... How well do you trust the file? Are you certain there will never be anything malicious in there? It might be safer to expand things yourself. HTH, Charles
        Message 3 of 4 , Aug 2, 2006
        • 0 Attachment
          Arun Pandey wrote:
          : "brendangibson" <brendangibson@...> wrote:

          :: If I have a string like '1..10,12..20', how can I use that as a
          :: list?
          ::
          :: I want to do something like:
          ::
          :: my $string = '1..10,12..20'; # Is read from a file, ...

          : I think you should use eval function, i.e create a code
          : dynamicaly using the string and then eval it.

          How well do you trust the file? Are you certain there will
          never be anything malicious in there? It might be safer to
          expand things yourself.


          HTH,

          Charles K. Clarkson
          --
          Mobile Homes Specialist
          Free Market Advocate
          Web Programmer

          254 968-8328

          Don't tread on my bandwidth. Trim your posts.
        • Rob Biedenharn
          ... Are you expecting this: 1..10,12..20 to become the list: (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 17, 18, 19, 20) perl -le my $string =
          Message 4 of 4 , Aug 2, 2006
          • 0 Attachment
            On Aug 2, 2006, at 11:07 AM, Charles K. Clarkson wrote:

            > Arun Pandey wrote:
            > : "brendangibson" <brendangibson@...> wrote:
            >
            > :: If I have a string like '1..10,12..20', how can I use that as a
            > :: list?
            > ::
            > :: I want to do something like:
            > ::
            > :: my $string = '1..10,12..20'; # Is read from a file, ...
            >
            > : I think you should use eval function, i.e create a code
            > : dynamicaly using the string and then eval it.
            >
            > How well do you trust the file? Are you certain there will
            > never be anything malicious in there? It might be safer to
            > expand things yourself.
            >
            > HTH,
            >
            > Charles K. Clarkson
            > --

            Are you expecting this: '1..10,12..20'
            to become the list: (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15,
            16, 17, 18, 19, 20)

            perl -le 'my $string = "1..10,12..20";
            print join(q{,},eval($string)) if $string =~ m/^(\d+\.\.\d
            +,?)+$/xms;'

            You can take it from there, yes?

            -Rob



            [Non-text portions of this message have been removed]
          Your message has been successfully submitted and would be delivered to recipients shortly.