Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Newbie to PERL removing text from array

by s_baugh23 (Initiate)
on Feb 14, 2012 at 11:20 UTC ( #953680=perlquestion: print w/replies, xml ) Need Help??
s_baugh23 has asked for the wisdom of the Perl Monks concerning the following question:


Apologies in advance but im totally new to perl and im trying to write a script that does the following

im inputting a file with the following into an array

1610 0050.5694.0170 Yes 0 Gi2/4/4

* 3128 0050.569a.5232 Yes 5 Gi1/3/12

And I want to end up the following, Could anyone please point me in the right direction on how to achieve this.

005056940170 Gi2/4/4

0050569a5232 Gi1/3/12

  • Comment on Newbie to PERL removing text from array

Replies are listed 'Best First'.
Re: Newbie to PERL removing text from array
by moritz (Cardinal) on Feb 14, 2012 at 11:25 UTC
Re: Newbie to PERL removing text from array
by JavaFan (Canon) on Feb 14, 2012 at 12:47 UTC
    You say you're putting "a file" into an array; I presume you mean the content of the file. But there are many ways of doing this. Are you going to store the entire content into a single array element? Each paragraph into a different element? Each line? Each word? Each character? Something else?

    Assuming that what you are showing us are two different array elements, the following might work (untested):

    foreach (@array) { s/^[^0-9]*[0-9]+\s+//; s/\.//g; s/Yes [0-9] //; }
    But I've no idea how that works out of the rest of your file.
      Hi, Thanks for the advice, I'm reading the contents of a file my ultimate goal is achieve a sanatzied version before merging with another file.
Re: Newbie to PERL removing text from array
by Erez (Priest) on Feb 14, 2012 at 12:19 UTC

    This mainly depends on the file format. If you can be 100% sure that this, and only this is the way lines will be presented, then you can do the following:

    my @output; foreach my $line (@lines) { my @split = split ' ', $line; #split by whitespace pop @split if $split[0] eq '*'; #getting rid of leading * $split[0] =~ s/\.//g #remove the dots from the element push @output, $split[0] . ' ' . $split[3]; }

    Otherwise, you'll need to do a foreach on each of @split's elements, match them to a pattern and use those that you need.

    Principle of Least Astonishment: Any language that doesn’t occasionally surprise the novice will pay for it by continually surprising the expert

Re: Newbie to PERL removing text from array
by InfiniteSilence (Curate) on Feb 14, 2012 at 22:15 UTC

    There's was this guy outside of my local Little Caesar's pizza that shook a can with some quarters in it and asked for change. I like pizza so I would see this guy there all the time. After a while I kind of got tired of giving this guy money -- I mean, he didn't seem to be bettering his life at all. Finally I said I wouldn't do it anymore. He seemed pretty miffed about it but, you know what -- he's no longer outside of the pizza joint. He's moved on.

    What's the moral of the story? I think people who post requirements without code are like that guy with the can and Monks here are being foolish by writing code for them. I personally insist that people show me some work before I help them out. Maybe Perlmonks would be a better place without that rattling can noise. Just my two cents.

    Celebrate Intellectual Diversity

      Sorry you feel that way, but as I eluded to im new to this and was just looking for a few pointers.
Re: Newbie to PERL removing text from array
by ansh batra (Friar) on Feb 14, 2012 at 12:09 UTC
    dude seems like your requirement is not generalized(specific).
    you are filtering the numbers after first whitespace
    then in the second string you are taking alphabet 'a' also.
    and you are not taking the number after "Yes"
    generalize your problem and try to use regular expression to solve it.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://953680]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2017-10-24 02:51 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (286 votes). Check out past polls.