Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
XP is just a number
 
PerlMonks  

(ar0n) Re: grep help

by ar0n (Priest)
on Jan 22, 2002 at 04:19 UTC ( #140540=note: print w/ replies, xml ) Need Help??


in reply to grep help

It's an okay way, but you don't really need regular expressions to match the haircolor or the firstname, since you're just matching string constants ("brown" and "j").

And since the keys are just indices, it's probably better to use an array:

my @friends; for (@raw_list) { my @friend = split /,/; push @friends, { FIRSTNAME => $friend[0], LASTNAME => $friend[1], HAIRCOLOR => $friend[2] }; } my @results = grep { lc $_->{HAIRCOLOR} eq 'brown' and 'j' eq substr(lc $_->{FIRSTNAME}, 0, 1) } @friends;

The new line in the grep block, is, of course, only a style decision.


Update: Yes, I have tried it, and it works for me. What error is it giving?

Update 2: You've changed the format of the input file.

When splitting, spaces matter. Have you tried your own code with "john, smith, brown"? It doesn't work.

Anyhow, replace the split statement above with split /\s*,\s*/


[ ar0n -- want job (boston) ]


Comment on (ar0n) Re: grep help
Download Code
Re: (ar0n) Re: grep help
by sickboy on Jan 22, 2002 at 04:49 UTC
    Have u tried this code? It doesn't work for me...
      It's not getting an error.
      @raw_list = ("john, smith, brown","tim, jones, brown","Seth, Kel, blue +"); print @results;

      Add this stuff and see if @results has any info
      You have tried the code I came up with haven't you?
      It works.
      Have u tried this code? It doesn't work for me...


      That's pretty cool that i lost a point for stating the obvious.
      So tough guy's code doesn't really work, and i lose the point
      You know where those points can go, on my Permanent Record.
      I could care less.
Re: (ar0n) Re: grep help
by sickboy on Jan 22, 2002 at 08:06 UTC
    You are right, neither code works with the extra space.
    But what has me kind of curious

    Why did you have to change my example/question?
    Using an array?
    Can't figuire it out the correct way, no?


    See I am working with a rather large file, not too bad.
    Anyway i have multiple values, and I can't cut and paste that info in.
    It's work, so i make up these stupid friend examples, that look like homework examples.
    Which is pretty impressive now that I think about it.
    You thought it looked like textbook examples, thanks.

    Anyway Doogie, show me how you would do it my way.
    And if your line looks like mine, no problem I'm glad u learned something.

      Why did you have to change my example/question? Using an array? Can't figuire it out the correct way, no?

      Because using a hash is wasteful when you don't need it. You were storing the 'friends' in a hash keyed on the line number, which is unnecessary, since you can achieve the same thing more efficiently using an array.

      Like you said, you're using a rather large file. Hashes take up more memory than arrays, which can be quite costly when you're dealing with big files (When you deal with 100 MB logfiles, you learn these things).

      So in all honesty, no, yours was not the correct way.

      You thought it looked like textbook examples, thanks.

      I'm not sure how that would be considered a compliment, but okay. You're welcome.

      Anyway Doogie, show me how you would do it my way.

      1. Is 'Doogie' another insult? (I really don't know what it means)
      2. I did show you. Use split /\s*,\s*/ instead of split /,/

      And if your line looks like mine, no problem I'm glad u learned something.

      Do you actually want me to help you? If so, antagonizing people doesn't help.

      If you don't want me to help you, feel free to say so. I have enough things to do.

      [ ar0n -- want job (boston) ]

        actually after looking at your code, I don't want your help.
        You made a big deal about it last night, and you couldn't even really
        grep out the info with the data stored the way it was.

        Nice try though, spinning it all around.
        Now you can just go away.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://140540]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2014-04-18 01:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (460 votes), past polls