newbie help - finding all substrings with index?

Discussion in 'Perl Misc' started by kadau, Mar 25, 2005.

  1. kadau

    kadau Guest

    Hi :)

    I hope this is an appropriate group to post in, my apologies if it is
    not. I would to to be able to read from a file and be able to locate all
    the instances of a substring within it, save it in an array, then print
    it out. I am fine with this except finding all the matches. Could anyone
    explain how to do this with index? Or is there some other way (please
    try to keep it simple if you can :)
    Here is the text file ive been given

    __

    #Transaction code

    S,M,L

    #Sales commission

    5:7:10

    #Retail price items

    *1<1002.00<
    *2<125.00<
    *3<61864.35<
    *4<890876.99<
    *5<9.99<

    __

    I want to locate & save to an array all lines begging with "*". I
    presume I will then be able to use split to get the raw prices out of
    the array (in between the "<").

    Thanks to anyone who can help.
     
    kadau, Mar 25, 2005
    #1
    1. Advertising

  2. kadau <brettaw@NO_spam_gmail.com> wrote in news:MPG.1caea6738a31c9e8989680
    @news.tpg.com.au:

    > it out. I am fine with this except finding all the matches. Could
    > anyone explain how to do this with index? Or is there some other
    > way (please try to keep it simple if you can :)


    What have you tried?

    Have you read the documentation?

    perldoc perlre
    perldoc perlretut

    Please consult the posting guidelines for this group for information on
    how to help others help you.

    Sinan
     
    A. Sinan Unur, Mar 25, 2005
    #2
    1. Advertising

  3. kadau wrote:

    > I hope this is an appropriate group to post in, my apologies if it is
    > not. I would to to be able to read from a file and be able to locate all
    > the instances of a substring within it, save it in an array, then print
    > it out.


    Are you sure that's _what_ you want to do and not _how_ you think you
    can do something? Anyhow I think you are expressing what you want
    carefully. In programming, precisely understanding what it is you want
    to do is often 90% of doing it.

    > I am fine with this except finding all the matches. Could anyone
    > explain how to do this with index?


    To do that you'd need to slurp the whole file into a string (see FAQ)
    but this is far form a natural approach.

    my $pos=0;
    while ( ( $pos = index( $file_content, $target, $pos )) > -1 ) {
    # Do stuff with $file_content and $pos
    }

    > Or is there some other way (please
    > try to keep it simple if you can :)


    Yes there are much simpler ways. But first you need to step back and
    consider what you really want to do.

    > I want to locate & save to an array all lines begging with "*".


    Hmmm... ok now we are perhaps getting closer.

    my @lines_starting_with_asterisk;
    local *_;
    while(<FILE>) {
    push @lines_starting_with_asterisk => $_ if /^\*/;
    }

    > I
    > presume I will then be able to use split to get the raw prices out of
    > the array (in between the "<").


    But why do you think you need an array? Can you not simply process the
    stuff as you read it from the file?

    The split() function is good if you have a delimited list of arbitrary
    length but the usual way to structured text in Perl is just the simple
    pattern match. Simply write a pattern that matches the lines you are
    interested in and captures the interesting parts of the line.

    local *_;
    while(<FILE>) {
    next unless my ($item, $price)=/^\*(.*?)<(.*?)</;
    # Do stuff with $item and $price.
    }
     
    Brian McCauley, Mar 25, 2005
    #3
  4. Brian McCauley wrote:

    > Anyhow I think you are expressing what you want carefully.


    Opps, should have said "... _not_ expressing...".

    Gee, that's kinda embarrasing. :)
     
    Brian McCauley, Mar 25, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    665
    Thorsten Kampe
    Jul 13, 2006
  2. DarthBob88

    Finding and Replacing Substrings In A String

    DarthBob88, Sep 23, 2007, in forum: C Programming
    Replies:
    7
    Views:
    566
    Keith Thompson
    Sep 23, 2007
  3. Robert Reno

    How to get all possible substrings

    Robert Reno, Jan 26, 2010, in forum: C++
    Replies:
    4
    Views:
    568
    P. Lepin
    Jan 28, 2010
  4. puzzlecracker

    enumerate all adjecent substrings in the file

    puzzlecracker, Dec 11, 2005, in forum: Perl Misc
    Replies:
    9
    Views:
    132
    Anno Siegel
    Dec 13, 2005
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    307
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page