Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Help in using arrays

by rsriram (Hermit)
on Mar 14, 2007 at 08:32 UTC ( [id://604751]=perlquestion: print w/replies, xml ) Need Help??

rsriram has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Help in using arrays
by Corion (Patriarch) on Mar 14, 2007 at 08:38 UTC

    The code seems very similar to your code in Searching and printing through a array - did my reply not work for you?

    If you can tell me where my reply does not work for you I can maybe help you to adapt my code to your problem. It would also help to have a complete, self-contained program which initializes @all and reads the data from a __DATA__ section so we can run the code and reproduce the problem without any further setup on our part. Something like this:

    #!/usr/bin/perl -w use strict; my @all = ("7723","7725","9908","7765","7874"); while (<DATA>) { # ... your code to check the current line }: __DATA__ 77876 8543 CA84985 54E 77873 8003 CA84985 54E 77875 7725 CA84985 54E ...
Re: Help in using arrays
by davorg (Chancellor) on Mar 14, 2007 at 08:51 UTC

    If you are going to revisit a previous question then you should, at least, include a link to the earlier discussion so that we can see what advice has already been offered and ignored.

    Update: I've now taken a closer look at your code and I can't see the problem that you are having. The code below is based on your code above and has been expanded to create a standalone program. It seems to work as expected and print out the whole line. I therefore suspect that your problem is in the bits of your code that you aren't showing us.

    #!/usr/bin/perl use strict; use warnings; my @all = qw(7723 7725 9908 7765 7874); while (<DATA>) { for my $x (0..$#all) { if($_ =~ /$all[$x]/) { print $_; } } } __DATA__ 77876 8543 CA84985 54E 77873 8003 CA84985 54E 77875 7725 CA84985 54E 77872 8511 CA84985 54E 77873 8123 CA84985 54E 77822 9908 CA84985 54E 77819 8503 CA84985 54E 77826 8040 CA84985 54E 77822 7874 CA84985 54E 77884 8543 CA84985 54E 77809 7211 CA84985 54E

    And the output I get is:

    77875 7725 CA84985 54E 77822 9908 CA84985 54E 77822 7874 CA84985 54E

    There is, of course, a potential bug in this code as it checks for the existance of your numbers anywhere in a line of input and you specifically want to check for it in the second column. You can therefore change the central loop of my code to look lie this (and note that I've also been able to change the match operator to an equality operator).

    while (<DATA>) { my $code = (split)[1]; for my $x (0..$#all) { if($code eq $all[$x]) { print $_; } } }

    Finally, there's no point in checking for more than one match, so it's worth exiting the inner loop once a match is found.

    while (<DATA>) { my $code = (split)[1]; for my $x (0..$#all) { if($code eq $all[$x]) { print $_; last; } } }
      Your code suggestion:
      while (<DATA>) { my $code = (split)[1]; for my $x (0..$#all) { if($code eq $all[$x]) { print $_; last; } } }
      I suppose you have written it this way to stay close to the OP's coding style, but it should be mentioned that indexing the @all array is not necessary and not particularly perlish.
      while (<DATA>) { my $code = (split)[1]; for my $x ( @all ) { if($code eq $x) { print $_; last; } } }
      Now it becomes apparent that what the loop does is really a grep operation:
      while (<DATA>) { my $code = (split)[1]; print if grep $_ eq $code, @all; } }
      Finally, it turns out that grep can be replaced with a hash lookup when the list @all is replaced by a hash:
      my %all = map +( $_ => 1), qw(7723 7725 9908 7765 7874); $all{ (split)[ 1]} and print while <DATA>;
      Anno

      Update: Stylistic change, pomposity tuned down a notch.

Re: Help in using arrays
by johngg (Canon) on Mar 14, 2007 at 09:48 UTC
    You can set up a regular expression that will match any of the elements of @all then read each line and split, passing the original line and each column out of a map to grep the second columns that match, finally maping out the original line and printing it.

    use strict; use warnings; my @wanted = qw{7723 7725 9908 7765 7874}; my $rxExtract; { local $" = q{|}; $rxExtract = qr{\b@wanted\b}; } print map { $_->[0] } grep { $_->[2] =~ $rxExtract } map { [ $_, split ] } <DATA>; __END__ 77876 8543 CA84985 54E 77873 8003 CA84985 54E 77875 7725 CA84985 54E 77872 8511 CA84985 54E 77873 8123 CA84985 54E 77822 9908 CA84985 54E 77819 8503 CA84985 54E 77826 8040 CA84985 54E 77822 7874 CA84985 54E 77884 8543 CA84985 54E 77809 7211 CA84985 54E

    The output is

    77875 7725 CA84985 54E 77822 9908 CA84985 54E 77822 7874 CA84985 54E

    I hope this is of use.

    Cheers,

    JohnGG

Re: Help in using arrays
by radiantmatrix (Parson) on Mar 15, 2007 at 14:02 UTC

    It seems to me like you're over-thinking your algorithm. What you want to express in code is "for each line of data, if the value in the second column is contained in my pre-existing dataset (@all), print the line". Perl is very expressive, so it allows you to say that almost directly:

    #!/usr/bin/env perl use strict; use warnings; @all = ("7723","7725","9908","7765","7874"); while( <DATA> ) { my @column = split /\s+/; #get columns out of data line for my $x (@all) { next unless $x == $column[1]; # compare second column, move on i +f no match print $_; # we found a match, print out the original line last; # we're done, so stop comparing this line } } __DATA__ 77876 8543 CA84985 54E 77873 8003 CA84985 54E 77875 7725 CA84985 54E 77872 8511 CA84985 54E 77873 8123 CA84985 54E 77822 9908 CA84985 54E 77819 8503 CA84985 54E 77826 8040 CA84985 54E 77822 7874 CA84985 54E 77884 8543 CA84985 54E 77809 7211 CA84985 54E

    However, a knowledge of design patterns yields a more elegant solution suggested by the Schwartzian Transform.

    #!/usr/bin/env perl use strict; use warnings; @all = ("7723","7725","9908","7765","7874"); # partial application of the Schwartzian Transform # each unique value in @all will now be a key in the %all hash my %all = map { $_ => 1 } @all; while( <DATA> ) { my @column = split /\s+/; #get columns out of data line print $_ if $all{$column[1]}; } __DATA__ 77876 8543 CA84985 54E 77873 8003 CA84985 54E 77875 7725 CA84985 54E 77872 8511 CA84985 54E 77873 8123 CA84985 54E 77822 9908 CA84985 54E 77819 8503 CA84985 54E 77826 8040 CA84985 54E 77822 7874 CA84985 54E 77884 8543 CA84985 54E 77809 7211 CA84985 54E

    The transformation of array to hash allows the check to be very simple: "does our dataset contain this value?".

    <radiant.matrix>
    Ramblings and references
    The Code that can be seen is not the true Code
    I haven't found a problem yet that can't be solved by a well-placed trebuchet

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://604751]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-26 07:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found