Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default understanding code

    Hi all,

    Can any one walk me thru this piece of code ::

    while(<STDIN>)
    {
    chomp ;
    $isbn =(split(/^_/, $_))[0] ; --- not able to understand what is
    being accessed (......)[0]
    unless ($KEYS{$isbn} ) ---- isbn is a scalar variable, how keys
    wok on it ?
    {
    print "$_\n" ;
    $KEYS{$isbn} =1 ;
    }
    }


    Thanks,

    Nilay Puri Guest

  2. Similar Questions and Discussions

    1. Needs help understanding the product
      Is Flash Media Server 2 an actual server that you buy and install on your computer to host your flash files? I'm really confused in that the server...
    2. A little understanding
      I am fairly new to all of this so please bare with me... 1. With this flash streaming server, does anyone know if it is possible to upload any type...
    3. Help understanding code...
      I've just picked up a more advanced book on PHP and it has a lot of example code in it. I understand most of it but some things I'm seeing I don't...
    4. [PHP] Help understanding code...
      ! means not, for example $yes != $no Regarding the (xxx) ? x : x; Your assumption is correct I use it alot, but sometimes it's still better to...
    5. Help understanding MAP
      After reading llama, camel, perldoc and searching web for explainations, I just don't get how map works. I'm sure it is something simple. I'm...
  3. #2

    Default Re: understanding code

    Hi,

    Nilay Puri, Noida wrote:
    >Hi all,
    >
    >Can any one walk me thru this piece of code ::
    >
    >while(<STDIN>)
    >{
    > chomp ;
    > $isbn =(split(/^_/, $_))[0] ; --- not able to understand what is
    >being accessed (......)[0]
    That's the first element of the array returned by split (you can access array elements like this:

    @array = (1,2,3);
    $number = $array[0]
    print $number;

    This prints "1".
    > unless ($KEYS{$isbn} ) ---- isbn is a scalar variable, how keys
    >wok on it ?
    Obviously, there is a hash named %KEYS, where each key is an isbn number.

    HTH,

    Jan
    --
    Either this man is dead or my watch has stopped. - Groucho Marx
    Jan Eden Guest

  4. #3

    Default RE: understanding code

    "Nilay Puri, Noida" <nilayp@noida.hcltech.com> wrote:
    >Can any one walk me thru this piece of code ::
    >
    >while(<STDIN>)
    >{
    > chomp ;
    > $isbn =(split(/^_/, $_))[0] ; --- not able to understand what is
    >being accessed (......)[0]
    > unless ($KEYS{$isbn} ) ---- isbn is a scalar variable, how keys
    >wok on it ?
    > {
    > print "$_\n" ;
    > $KEYS{$isbn} =1 ;
    > }
    >}
    I'm not sure what the intent of the code is, but I would guess you're parsing a set of lines from a file, each line containing an ISBN and some other data, to extract all the unique ISBNs from it, and print those numbers without repeating duplicates, as a side effect leaving you with a hash containing all those unique ISBNs.

    It would be useful to see some of the data the code is intended to process.

    Assuming that's the goal, you're not far from it. The (......)[0] says "take the result of the split, which is a list, and get the 0th or first element from it". This works because you can subscript a list the same way you can subscript an array variable. In other words, the expression gets the first thing from the list that results from splitting, which is probably meant to be the first thing on the line, which is probably meant to be the ISBN.

    The $KEYS{$isbn} expression is a hash access, getting from hash %KEYS the value associated with key $isbn. You're right, 'keys' is a function which is used with hashes, but in this case KEYS is also a variable.

    The split pattern seems unusual. As written, it says "split the string at the beginning if the line starts with an underscore". The caret character in the pattern will match on the start of the string; the string will consist of an entire line read from the file, put into the variable $_ by the read operation <STDIN>. I've never tried to split on the beginning of the string, so let's write a test script that does that and see what happens.

    testsplitBOL.pl
    ---------------
    use warnings;
    use strict;
    my @result;
    while(<STDIN>){
    chomp;
    print "Processing line: >$_<\n";
    @result = split( /^_/, $_);
    print "++Split resulted in ", scalar(@result), " items.\n";
    print "++First element of split is >", $result[0], "<.\n";
    }

    result:
    -------

    D:\MCD\dvl\scripts>type testsplitBOL.txt
    1234 some text
    _5678 some other text

    _9012_some_other_text_separated_by_underscores
    _7654
    0987

    D:\MCD\dvl\scripts>type testsplitBOL.txt | perl testsplitBOL.pl
    Processing line: >1234 some text<
    Split resulted in 1 items.
    First element of split is >1234 some text<.
    Processing line: >_5678 some other text<
    Split resulted in 2 items.
    First element of split is ><.
    Processing line: ><
    Split resulted in 0 items.
    Use of uninitialized value in print at testsplitBOL.pl line 11, <STDIN> line 3.
    First element of split is ><.
    Processing line: >_9012_some_other_text_separated_by_underscores<
    Split resulted in 2 items.
    First element of split is ><.
    Processing line: >_7654<
    Split resulted in 2 items.
    First element of split is ><.
    Processing line: >0987<
    Split resulted in 1 items.
    First element of split is >0987<.

    From these results we can see several things:

    - Splitting on the beginning of the string, when successful, appears to give you an empty string as the first elem of the resulting list. Your code would take this to be an ISBN and use it as a hash key, which is certainly not correct.
    - When the split does not match its pattern, it yields a list consisting of a single element, the original string.
    - The pattern in split only matches lines that begin with underscore. Whether or not that's what you want depends on your data.
    - The code should have a test to make sure the line is not just an empty string

    Note that the Perl documentation for split says

    A PATTERN of /^/ is treated as if it were /^/m, since it isn't much use otherwise

    but that doesn't seem to apply here, both because your pattern is not /^/ (rather, it is /^_/) and because that doesn't seem to be what's happening in the test results.

    I'd be glad to help you code up your loop, but we really need to see a sample of data to understand the task. In any event, I think you want something like this:

    use warnings;
    use strict;

    my %KEYS = ();
    my $isbn;
    my @result;

    while(<STDIN>){
    chomp;
    if( /^_/ ){ # select only those lines to split: not empty and start with underscore, or whatever
    @result = split( /:/, $_); # split on whatever separates ISBN from what follows it on line
    if( scalar( @result ) > 1 ){ # make sure the split actually split something
    $isbn = $result[0]; # we assume the ISBN is first thing on the line
    unless( $KEYS{$isbn} ){ # make sure this ISBN hasn't already been printed before printing it
    print "$_\n";
    $KEYS{$isbn} = 1;
    }
    }
    else {
    die "Error processing line $.: $_ could not be split.\n";
    }
    }
    }

    Or something like that. You could make it more concise, but that's the basic idea. Show us your data!




    __________________________________________________ ________________
    New! Unlimited Netscape Internet Service.
    Only $9.95 a month -- Sign up today at [url]http://isp.netscape.com/register[/url]
    Act now to get a personalized email address!

    Netscape. Just the Net You Need.
    mcdavis941@netscape.net Guest

  5. #4

    Default Re: understanding code

    Nilay Puri wrote:
    >
    > Can any one walk me thru this piece of code ::
    >
    > while(<STDIN>)
    > {
    > chomp ;
    > $isbn =(split(/^_/, $_))[0] ; --- not able to understand what is
    > being accessed (......)[0]
    > unless ($KEYS{$isbn} ) ---- isbn is a scalar variable, how keys
    > wok on it ?
    > {
    > print "$_\n" ;
    > $KEYS{$isbn} =1 ;
    > }
    > }
    Hi Nilay.

    From your question, I guess the code isn't your own? I've put some whitespace in
    so that it's a bit more visible, like this:

    while (<STDIN>) {

    chomp;

    $isbn = (split(/^_/, $_))[0]; # --- not able to understand what is being accessed (......)[0]

    unless ( $KEYS{$isbn} ) { # ---- isbn is a scalar variable, how keys wok on it ?

    print "$_\n";

    $KEYS{$isbn} = 1;
    }
    }

    First of all, the line

    $isbn = (split(/^_/, $_))[0];

    is almost certainly not what was meant. It will put into $isdn either a
    null string (if the first character of the record is an underscore) or
    else a copy of the entire record. I can't guess what the interntion was.

    As for the line that accesses the %KEYS hash, you're probably best off
    reading

    perldoc perldata

    about how strings can be used to index hashes.

    HTH,

    Rob


    Rob Dixon Guest

  6. #5

    Default Re: understanding code

    Jan Eden wrote:
    >
    > Nilay Puri, Noida wrote:
    > >
    > >Can any one walk me thru this piece of code ::
    > >
    > >while(<STDIN>)
    > >{
    > > chomp ;
    > > $isbn =(split(/^_/, $_))[0] ; --- not able to understand what is
    > >being accessed (......)[0]
    >
    > That's the first element of the array returned by split
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    Functions/subroutines in perl return a LIST, not an array.

    perldoc -q "What is the difference between a list and an array"


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  7. #6

    Default Re: understanding code


    On 31 Jan 2004, at 21:40, John W. Krahn wrote:
    > Jan Eden wrote:
    >>
    >> Nilay Puri, Noida wrote:
    >>>
    >>> Can any one walk me thru this piece of code ::
    >>>
    >>> while(<STDIN>)
    >>> {
    >>> chomp ;
    >>> $isbn =(split(/^_/, $_))[0] ; --- not able to understand what is
    >>> being accessed (......)[0]
    >>
    >> That's the first element of the array returned by split
    > ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    > Functions/subroutines in perl return a LIST, not an array.
    >
    > perldoc -q "What is the difference between a list and an array"
    >
    Nice, however, for those Learning Perl, it would be nice to have a
    simple description first, it is difficult to understand the perldoc
    definition if you don't know the basic difference to start with.

    From Learning Perl 2's:

    A list is ordered scalar data # (one, two, three, four)
    An array is a variable that holds a list # @array = (one, two, three,
    four);
    Each element of the array is a separate scalar variable with an
    independent scalar value # $array[1] is two
    These values are ordered # $array[0] $array[1] ....2 .. 3.

    And of course once you've read that, you can do: perldoc -q "What is
    the difference between a list and an array"

    With the Perl heavyweights on this list it is sometimes easy to forget
    that so many are LEARNING Perl and for most of us, we are always
    Learning Perl.

    Cheers,

    Jerry




    Mac Os X @ Rocteur . Cc Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139